投稿

ラベル(AppleScript)が付いた投稿を表示しています

Automatorの変数をAppleScript内で使う

Quick Edit Pencil
Automatorで設定した変数をAppleScript内で変数名を指定することはできない
なので、AppleScriptでinputの値を変数に格納する
on run {input, parameters} set val1 to item 1 of input set val2 to item 2 of input set cnt to (count of input) end run
参考サイト AutomatorでAppleScriptに値を渡す http://ameblo.jp/softwaredeveloper/entry-11988473204.html AppleScriptのサンプル
on run {input, parameters} set pictFol to path to pictures folder from user domain as text --ピクチャフォルダのパスを取得 set searchCha to item 1 of input --Automatorで受け渡された変数値(ファイル名)を取得 set the clipboard to searchCha --クリップボードにコピー return input end run

AppleScript:: SafariのDOM操作 > 要素の削除 id属性を指定 class属性

Quick Edit Pencil
tell application "Safari"
    tell document 1

        do JavaScript "
    //要素IDの削除
    var remove = ['acWrHeader','abth_tp','booth2_closedinfo','abth_lft','abth_rght','abth_mdl']
    for (i = 0; i < remove.length; i++) {
      if(document.getElementById(remove[i])){
        document.getElementById(remove[i]).innerHTML=''
      }
    }
    //要素imageinfoサムネイルCLASS = の削除
    document.getElementById('imageinfo').getElementsByClassName('pts01')[0].innerHTML='';
    "
        do JavaScript "
    var imagesrc = new Array();

    //imageinfo
    var imageinfo_pts02_imgs = document.getElementById('imageinfo').getElementsByClassName('pts02')[0].getElementsByTagName('img');
    for (i = 0; i < imageinfo_pts02_imgs.length; i++) {
    img = imageinfo_pts02_imgs[i];
    imagesrc.push(img.src);
    }

    //imageinfo
    var adoc_imgs = document.getElementById('adoc').getElementsByTagName('img');
    for (i = 0; i < adoc_imgs.length; i++) {
    img = adoc_imgs[i];
    imagesrc.push(img.src);
    }
    "
        set imagesrcs to do JavaScript "imagesrc"
        return imagesrcs
    end tell

end tell

AppleScriptでURLエンコード

Quick Edit Pencil

AppleScriptでJavaScriptを使わずにURLエンコードする。Perl, Ruby

set theText to "http://ja.wikipedia.org/wiki/アップルスクリプト"

 

--Perl

set theURL to do shell script "perl -MURI::Escape -wle 'print uri_escape(\"" & theText & "\")'"

log "2=" & theURL

(*2=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

 

--perl

set theURL to do shell script "perl -MEncode -MURI::Escape -e 'print uri_escape(encode('utf8', decode('utf8', \"" & theText & "\")))'"

log "3=" & theURL

(*3=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

 

--ruby

--AppleScript : URLエンコード http://blog.goo.ne.jp/vallie/e/26f5d6333f44e071863c173300865306

set theURL to do shell script "ruby -r cgi -e 'puts CGI.escape(\"" & theText & "\")'"

log "4=" & theURL

(*4=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

 

--URL全体をエンコードしたいときは、上記のエンコード方法では有効なURLとならないので、有効なURLとしてエンコードしておきたいときは、次をかませる。

 

--有効なURL

set theURL to do shell script "echo " & quoted form of theURL & " | sed -e 's/%23/#/g' -e 's/%24/$/g' -e 's/%26/&/g' -e 's/%2B/+/g' -e 's/%2C/,/g' -e 's/%2F/\\//g' -e 's/%3A/:/g' -e 's/%3B/;/g' -e 's/%3D/=/g' -e 's/%3F/?/g' -e 's/%40/@/g'"

logtheURL

(*http://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

 

--デコード

set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(decodeURI(\"" & theURL & "\"))'"

logtheURL

(*http://ja.wikipedia.org/wiki/アップルスクリプト*)


AppleScriptでJavaScriptを使いたい

AppleScriptとターミナルでJavaScriptを使う

Quick Edit Pencil
AppleScriptでJavaScriptを使いたい
ターミナルでJavaScriptを使いたい

AppleScriptでJavaScriptを使いたい

applescript jsc javascriptcore

具体には、URL全体をエンコード(encodeURI)しておきたい時


下記のように、Sfafariを経由して、JavaScriptを使う事ができるが、Safariが起動してまうし、Safariが起動していてもウィンドウがゼロの時はエラーが出るので、スマートではない。


set theText to "アップルスクリプト"
--JavaScript
tell application "Safari"
set theURL to (do JavaScript "encodeURI(\"" & theText & "\")" in document 1) as text
log "encode=" & theURL
(*0=%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)
set theURL to (do JavaScript "decodeURI(\"" & theText & "\")" in document 1) as text
log "decode=" & theURL
(*0=アップルスクリプト*)
end tell

 

そこで、do shell scriptJavaScriptCore.frameworkを直接呼んで、URL全体をエンコード(encodeURI)。

encodeURIとencodeURIComponentは上手くエンコードしてれない。のでURL全体をエンコードしたい場合はリンクを参照。BWNSUY: AppleScriptでURLエンコード

しかし、Safariを通さずJavaScriptを使えるので今後、何かの役にたつかも知れない。
キモは
print([native code])
ターミナルでPATHを通すことで下記とできる。自分のMacだけで使う時はこれでOK。
 do shell script "jsc -e 'print(eccape(\"" & theText & "\"))'"

ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/bin
set theText to ""

--escapeでエンコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'var t=\"" & theText & "\";print(escape(t))'"
log "encode =" & theURL
(*encode =%E3%81%82*)
--デコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'var t=\"" & theURL & "\";print(decodeURI(t))'"
log "decode =" & theURL
(*decode =*)

--encodeURIでエンコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'var t=\"" & theText & "\";print(encodeURI(t))'"
log "encode =" & theURL
(*encode =%C3%A3%C2%81%C2%82*)
--デコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'var t=\"" & theURL & "\";print(decodeURI(t))'"
log "decode =" & theURL
(*decode =あ*)

--encodeURIComponentでエンコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'var t=\"" & theText & "\";print(encodeURIComponent(t))'"
log "encode =" & theURL
(*encode =%C3%A3%C2%81%C2%82*)
--デコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'var t=\"" & theURL & "\";print(decodeURI(t))'"
log "decode =" & theURL
(*decode =あ*)

ターミナルでJavaScriptを使いたい

tarminal jsc
ターミナルでもencodeURIとencodeURIComponentは上手くエンコードしてれない。のでURL全体をエンコードしたい場合はリンクを参照。BWNSUY: AppleScriptでURLエンコード

ターミナルでURLをエンコードしてみる

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(encodeURI("http://ja.wikipedia.org/wiki/アップルスクリプト"))'
又は
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc encodeurl.js
ホームディレクトリに「encodeurl.js」を新規作成し下記を記述。
// encodeurl.js
(function(){
    print(encodeURI("http://ja.wikipedia.org/wiki/アップルスクリプト")); //標準出力は print 関数
}()); // コードは即時関数で実行させる

JavaScriptCoreへPATHを通す

1 - .bash_profileに記述する方法
ホームディレクトリの「.bash_profile」というファイルにJavaScriptCoreへのPATHを記述します。
alias jsc="/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
ターミナルで.bash_profileを開き上記を追記して保存。.bash_profileがない時は新規作成される。
vim ~/.bash_profile
変更した.bash_profileを反映させる
source ~/.bash_profile
2 - ターミナルでハードリンクまたはシンボリックリンクする方法
ターミナルでPATHを通す
ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin
.bash_profileへの操作、vimのコマンドについての参考サイト
ターミナルで試してみる
jsc -e 'print(encodeURI("http://ja.wikipedia.org/wiki/アップルスクリプト"))'
jsc runner.js

参考にしたサイト

AppleScriptとターミナルでJavaScriptを使う

Quick Edit Pencil

AppleScriptでJavaScriptを使いたい

applescript jsc javascriptcore

具体には、URLをエンコード(url encode)したい時、

set theText to "http://ja.wikipedia.org/wiki/アップルスクリプト"

--JavaScript

tell application "Safari"

set theURL to (do JavaScript "encodeURI(\"" & theText & "\")" in document 1) as text

log "0=" & theURL

end tell

(*0=http://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

と、Safariを経由して結果を得る方法が考えられるが、Safariを経由せず、ApplescriptでJavaScriptを使えないか探ってみた。

Safariで使われているJavaScriptCoreを直接操作する方法があった。

set theText to "http://ja.wikipedia.org/wiki/アップルスクリプト"

--JavaScript

set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(escape(\"" & theText & "\"))'"

log "1=" & theURL

(*1=http%3A//ja.wikipedia.org/wiki/%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)


キモは

print([native code])

ターミナルでPATHを通すことで下記とできる。自分のMacだけで使う時はこれでOK。

 

do shell script "jsc -e 'print(eccape(\"" & theText & "\"))'"


ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/bin

AppleScriptでJavaScriptを使わずにURLエンコードする。Perl, Ruby

--Perl

set theURL to do shell script "perl -MURI::Escape -wle 'print uri_escape(\"" & theText & "\")'"

log "2=" & theURL

(*2=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

 

--perl

set theURL to do shell script "perl -MEncode -MURI::Escape -e 'print uri_escape(encode('utf8', decode('utf8', \"" & theText & "\")))'"

log "3=" & theURL

(*3=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

 

--ruby

--AppleScript : URLエンコード http://blog.goo.ne.jp/vallie/e/26f5d6333f44e071863c173300865306

set theURL to do shell script "ruby -r cgi -e 'puts CGI.escape(\"" & theText & "\")'"

log "4=" & theURL

(*4=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)

ターミナルでJavaScriptを使いたい

tarminal jsc

ターミナルでURLをエンコードしてみる

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(escape("http://ja.wikipedia.org/wiki/アップルスクリプト"))'

又は

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc encodeurl.js

 

ホームディレクトリに「encodeurl.js」を新規作成し下記を記述。

// encodeurl.js

(function(){

    print(escape("http://ja.wikipedia.org/wiki/アップルスクリプト")); //標準出力は print 関数

}()); // コードは即時関数で実行させる

 

JavaScriptCoreへPATHを通す

1 - .bash_profileに記述する方法

ホームディレクトリの「.bash_profile」というファイルにJavaScriptCoreへのPATHを記述します。

alias jsc="/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"

ターミナルで.bash_profileを開き上記を追記して保存。.bash_profileがない時は新規作成される。

vim ~/.bash_profile

変更した.bash_profileを反映させる

source ~/.bash_profile

2 - ターミナルでハードリンクまたはシンボリックリンクする方法

ターミナルでPATHを通す

ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin

.bash_profileへの操作、vimのコマンドについての参考サイト

ターミナルで試してみる

jsc -e 'print(escape("http://ja.wikipedia.org/wiki/アップルスクリプト"))'

jsc encodeurl.js

参考にしたサイト

入手先をSpotlightコメントへ

Quick Edit Pencil

ダウンロード先をSpotlightコメントに入力してくれるApple Script « tybx

on adding folder items tothisFolderafter receivingsomeItems

if (count items of someItems) ≠ 1 then return

set itemAlias to (item 1 of someItems) as alias

do shell script "mdls " & (quoted form of (POSIX path of itemAlias))

set urls to the result

set oldDelimiters to AppleScript's text item delimiters

set AppleScript's text item delimiters to {"kMDItemWhereFroms"}

try

set urls to (text item 2 of urls)

set AppleScript's text item delimiters to {quote}

 

if (count urls's text items) ≤ 3 then

set urls to (text item 2 of urls)

else

set urls to (text item 4 of urls)

end if

 

set myFile to itemAlias as string

tell application "Finder"

set the comment of file myFile to urls

end tell

on error

set AppleScript's text item delimiters to oldDelimiters

return -- urls don't exist

end try

end adding folder items to

AppleScriptの結果を、Safariで表示するサンプルスクリプト

Quick Edit Pencil

AppleScriptのダイアログでは表示しきれない情報をSafariに表示するサンプルスクリプト

-- 結果の表示はSfafariを利用します。

仮想ドキュメント参照方法 

-- 存在しないファイルを参照すると、Safariは「ページを開けませんでした」を返す。
-- このキュメントをJavascript(DOM)で書き換えます。

実在ドキュメント参照方法

-- HTMLドキュメントを、一時ファイルとして毎回新規作成します。
-- このキュメントをSafariで参照し、Javascript(DOM)で書き換えます。
-- 新規作成されたHTMLドキュメントはスクリプト終了時に削除されます。

ファインダーでファイルを選択し、スクリプトを実行します。
複数のファイルを選択した場合は、タブで開く。 

--設定項目

set whichDialog to 0 --結果表示用HTMLドキュメント 0:仮想 1:実在

--以上設定

 

tell application "Finder"

set theFiles to selection

if theFiles = {} then

display dialog "Finder上で何もファイルが選択されていません"

return

end if

end tell

 

(*結果表示用HTMLの設定*)

tell application "Finder"

 

(*テンプレート*)

set plainHtml to "<!DOCTYPE html><html lang=\"ja\"><head><meta charset=\"UTF-8\" /><title>ファイル情報:ファイル名</title></head><body></body></html>"

 

(*結果HTMLドキュメントの参照パスの定義*)

set p to POSIX path of ((path to me)'s folder as Unicode text) --スクリプトファイルが置かれた場所

set n to do shell script "echo " & quoted form of ((path to me)'s name as Unicode text) & " | perl -pe 's/(.*)\\..*?$/$1.html/'" --ファイル名:スクリプト名.html

 

(*結果の仮想時と実在時の分岐設定*)

if whichDialog is 0 then --0:仮想

set theURL to "file://.." & p & n --参照階層の定義

else --1:実在

set theURL to "file://" & p & n --参照階層の定義

do shell script "echo " & "" & " > " & quoted form of (p & n) --実在ファイルの書き出し

end if

 

(*解像度を取得:Safariの結果ウインドウ位置設定のためディスプレイ解像度を取得*)

set displayBounds to bounds of the window of the desktop

 

end tell

 

(*ファイル情報の取得*)

repeat with idx from 1 to (length of theFiles)

log "--------------- タブ " & idx & " ---------------"

set aFile to itemidx of theFiles

logaFile

set aPath to aFile as Unicode text

logaPath

 

--ファイル情報の取得

 

tell application "Finder"

(*ファイル名を取得*)

set aName to name of aFile

logaName

 

(*Spotlightコメントを取得*)

set aComment to comment of aFile as text

(*SpotlightコメントをHTML用に編集*)

logaComment

if (length of the aComment) is not 0 then --ある時

set aComment to do shell script "echo " & quoted form of aComment & " | sed -e 's/
/<br>/g'" --行区切り=ラインブレークを置換

set aComment to do shell script "echo " & quoted form of aComment & " | sed -e 's/" & return & "/<br>/g'" --改行=パラグラフブレークを置換

else --ない時

set aComment to ""

end if

logaComment

 

(*ファイルの入手先を取得*)

set ItemWhereFroms to do shell script "mdls -name kMDItemWhereFroms " & quoted form of POSIX path of aPath

logItemWhereFroms

if ItemWhereFroms contains "(null)" then --ない時:kMDItemWhereFroms = (null)

set ItemWhereFroms to ""

else --ある時

set ItemWhereFroms to do shell script "echo " & quoted form of ItemWhereFroms & " | perl -pe 's/^.*?\"(.*?)\".*?$/\\1/g'"

end if

logItemWhereFroms

end tell

 

(*ファイル情報をセット*)

set aFileInfo to "<h1>" & aName & "のファイル情報</h1>ファイルパス:<br>" & aPath & "<br><br>Spotlight コメント: <br>" & aComment & "<br><br>入手先: <br>" & ItemWhereFroms

 

--結果の表示

 

(*結果を開く*)

tell application "Safari"

 

if idx is 1 then --一番目のファイル

(*新規ウインドウを開く*)

makenewdocument

set bounds of window 1 to {0, 0, (item 3 of displayBounds) / 2, (item 4 of displayBounds)} --ウインドウの位置と大きさを指定

(*URLのエンコード*)

set theURL to (do JavaScript "encodeURI(\"" & theURL & "\")" in document 1) as text

set properties of document 1 to {URL:theURL}

delay 0.2

(*DOM操作で情報をページに表示*)

my dom(document 1, plainHtml, aName, aFileInfo)

delay 0.2

else --二番目以降のファイル

(*タブでを開く*)

makenewtab of window 1

set properties of tab idx of window 1 to {URL:theURL}

delay 0.2

(*DOM操作で情報をページに表示*)

my dom(tabidx of window 1, plainHtml, aName, aFileInfo)

delay 0.2

end if

 

end tell

 

end repeat

 

(*結果表示用HTMLドキュメントの削除*)

if whichDialog is 1 then

do shell script "rm " & quoted form of (p & n)

end if

 

--以下ユーザー関数サブルーチン

 

(*DOM操作*)

on dom(target, plainHtml, aName, aFileInfo)

tell application "Safari"

do JavaScript ("


//htmlテンプレート

document.write('" & plainHtml & "');

 

//ページタイトルの変更

document.title='" & aName & "のファイル情報';

 

//bodyにファイル情報

var element = document.createElement('pre'); 

element.id = 'info'; 

element.innerHTML = '" & aFileInfo & "'; 

element.style.backgroundColor = ''; 

var objBody = document.getElementsByTagName('body').item(0); 

objBody.appendChild(element);

 

") intarget

end tell

end dom

AppleScript メモ

Quick Edit Pencil

Finder

(新規作成)

Finder 最前面ウインドウのフォルダにテキストファイルを作成する

tell application "Finder"

if exists folder of the front window then

set i to folder of the front window

else

set i to desktop

end if

set p to quoted form of (POSIX path of (i as Unicode text))

set n to do shell script "date +%Y%m%d-%H%M%S.txt"

do shell script "touch " & p & n

end tell

サービスとして登録して、キーボードショートカット(cmd+ctrl+shift+N)を割り当ててみた。

削除の場合赤字touchrmにする。

do shell script "touch " & p & n

(追記,追加,編集)

[ AppleScript ] 変数の内容をテキストファイルに書き出すサンプル

デスクトップに「test.txt」と言う名前のファイルが存在しない場合、新規作成します。ファイルが既に存在していれば追記。

tell application "Finder"

set aText to ("日本語のサンプル文字列" & return)

set aTextFile to open for access ((desktop as text) & "test.txt") with write permission

set aEOF to get eof of aTextFile

try

writeaTextstarting at (aEOF + 1) toaTextFile

on error aErrorText

display dialogaErrorText

end try

close accessaTextFile

end tell


日本語を書き出す時、文字コードはどうなるんだろう?

 

 

Path

(ファイルパス,ファイル名,ディレクトリ,変換)

ファイルパスからファイル名や拡張子を自由に取り出す

AppleScript

自分自身のファイルパスを取得

path to me

--alias "Macintosh HD:Users:zari:Library:Scripts:test_path_to.me.scpt"

 

ファイル名を取得

tell application "Finder" to (path to me)'s name

--"test_path_to.me.scpt"

 

ディレクトリを取得

tell application "Finder" to (path to me)'s folder as text

--"Macintosh HD:Users:zari:Library:Scripts:"

 

UNIX形式のパスに変換

(path to me)'s POSIX path

--"/Users/zari/Library/Scripts/test_path_to.me.scpt"

 

AppleScript形式のパスに変換

POSIX file "/Users/zari/Library/Scripts/test_path_to.me.scpt"

--file "Macintosh HD:Users:zari:Library:Scripts:test_path_to.me.scpt"

 

application

(TextEdit)

新規文書 (ウィンドウ) を作成

set theText to "* " & ((current date) as string) & return -- 新規文書に書き込む文字列(ここでは日時)

tell application "TextEdit"

activate

if not (exists front document) then makenewdocument

if text of front document is "" then

set theDoc to front document

else

set theDoc to makenewdocument

end if

set text of theDoc to theText -- 文字列を書き込む

set properties of text of theDoc to {color:{65535, 0, 0}, size:12, font:"Osaka-Mono"} -- 文字のサイズなどを指定できる(この指定は維持されない)

end tell

Finderで選択中の画像ファイルを、新規文書 (ウィンドウ) を作成して貼付ける

tell application "Finder"

set theFiles to selection

end tell

set aFile to theFiles

set aPath to aFile as Unicode text


tell application "TextEdit"

activate

set NewDoc to (makenewdocument)

set name of front window to fname_ext

set text of NewDoc to return & return & return & aExif

makenewattachmentwith properties {file name:(aPath as alias)} at after the first character of document 1

end tell

 

サンプル集

AppleScript 結果をSafariで表示する

Quick Edit Pencil

参考になる

Safari 開く

結果をファイルとして出力

# コマンドの出力を file へ上書きする

command >file

# コマンドの出力を file へ追記する

command >>file

エンコード デコード

Javascript

URLを非表示のWebViewで表示してJavaScriptを実行して結果を返す。

-- theURLを非表示のWebViewで表示してページ表示theRunJSDelay秒後theJavaScriptを実行して結果を返す

-- theTimeoutSec経つと読み込み中でも待つのを終了してJavaScriptを実行して結果を返す。その際最初にtimeoutと出力する

結果を入れるファイルの作成

参考

その他

Mac OS X Exif 削除

Quick Edit Pencil

Automator / AppleScript を使い削除する方法

Perlで書かれたコマンドラインツール、ExifToolが必要

  1. ExifToolからMac OS X Packageをダウンロードしてインストールする。

方法は下記のリンクを参照

SpotlightコメントをExifとして記録する

Quick Edit Pencil

SpotlightコメントをExifへ

Spotlightコメントを書き込んだ画像ファイルをiPhotoライブラリに移す時、iPhotoライブラリにコピーされた画像からSpotlightコメントは削除されてしまうので、iPhotoに取り込む前にSpotlightコメントをExifにコピーする方法。

検索すると"Spotlight コメントをIPTCキーワードに.app"というアップルスクリプトを発見

使い方は、対象の画像をSpotlight コメントをIPTCキーワードに.appにドロップする。

スクリプト動作は、Spotlightコメントを取得して、do shell scriptからexiftoolを呼んでExifのXMPのSubjectタグと結合する。というもの。

これで、iPhotoに取り込んだ画像のキーワード欄に反映する。

自分の環境ではエラーが出るのでスクリプトを修正した

<<script>> uniqueList メッセージを認識できません。とエラーが出るので、"Spotlight コメントをIPTCキーワードに.app"をAppleScriptエディタで開き、下の方にある関数(ユーザー定義関数=ハンドラ=サブルーチン)を編集した。赤字部分の変数名 uniqueList を rUniqueList に変更。

(*リストから重複を削除*)

on uniqueList(theList)
set uniqueList to {}
repeat with aItem in theList
if not (aItem is in uniqueList) then
set uniqueList to uniqueList & aItem
end if
end repeat
return uniqueList
end uniqueList

"Spotlight コメントをIPTCキーワードに.app"を動作させるには、Perlで書かれたコマンドラインツールが必要

ExifToolからMac OS X Packageをダウンロードしてインストールする。

SpotlightコメントをExifへ.app

Quick Edit Pencil

Spotlightコメント、ファイルの入手先、Exifを、


まとめてExifIPTCへ書き込むAppleScript 


NewImage iPhoto: キーワード、説明として反映

ライブラリに登録した画像の、Spotlightコメント、ファイルの入手先は消されるが、Exif情報として保持された、Spotlightコメント、ファイルの入手先は、iPhoto上で編集できるキーワード、説明として引き継がれ、Spotlightでの検索の対象となり、iPhotoから書き出した画像のIPTC(Exif情報)にも反映される。 

 

NewImage Google ドライブ: コメントや入手先の内容はExifとして保持

 

 

NewImage ダウンロード(準備中)
 SpotlightコメントをExifへ.app (ドロップレット)

 

 

動作条件

Perlで書かれたコマンドラインツールが必要です。
Exiftoolから、Mac OS X Packageをダウンロードしてインストールしてください。

(開発環境: Mac OS X Lion 10.7.5)

使い方

ファイルをSpotlightコメントをExifへ.appにドロップしてください。
注:フォルダごとドロップすると上手く動作しません。

設定項目

Applescriptエディタで開き次の項目等を指定(デフォルト設定はiPhoto向けに最適化)

(*書き込みタグの設定、デフォルトはIPTCのみ*)
set Tag1 to "-exif:ImageDescription="
set Tag2 to " -iptc:Caption-Abstract=" & quoted form of aDescription
set Tag3 to " -xmp:Description="
set Tag4 to " -sep ',' -xmp:Subject="
set Tag5 to " -sep ',' -iptc:Keywords=" & quoted form of aKeywords
--保存タグを指定
set Tags to Tag1 & Tag2 & Tag3 & Tag4 & Tag5

(*保存形式*)
set outFile1 to "-overwrite_original" --上書き保存
set outFile2 to "" --複製保存。元ファイルは[ファイル名.拡張子]_originalとてし元の場所にバックアップされる。
set outFile3 to " -o %dMerge\\ Exif/%f%+c.%e " --別名で保存。[同じ場所]/merge exif/[ファイル名][連番].[拡張子]
--保存形式を指定
set outFile to outFile3

参考にしたサイトとスクリプト

Twishort / _a_q_: @tanuki_hirotta 返事書くよりtanuki_hir…よりダウンロードできる

AppleScript 改行 コード 置換

Quick Edit Pencil

改行を置換

returnキーはLF、ASCII character (10)と\nはLF、ASCII character (13)と\rはCR、controlキー + returnキーはラインブレーク

 

(*テスト文字列を変数にセット*)

set str to "1LINE(LF)" & (ASCII character (10)) & "2LINE(CR)" & (ASCII character (13)) & "3LINE(LineBrake)
3LINE_2"

log "★生str: " & str & ""

(*★生str: 1LINE(LF)

2LINE(CR)

3LINE(LineBrake)
3LINE_2*)

 

(*LF置換*)

set LFstr to do shell script "echo " & quoted form of str & " | perl -pe 's/\\n//g'"

log "LF置換str: " & LFstr & ""

(*LF置換str: 1LINE(LF)2LINE(CR)

3LINE(LineBrake)
3LINE_2*)

 

(*CR置換*)

set CRstr to do shell script "echo " & quoted form of str & " | perl -pe 's/\\r//g'"

log "CR置換str: " & CRstr & ""

(*CR置換str: 1LINE(LF)

2LINE(CR)3LINE(LineBrake)
3LINE_2*)

 

(*ラインブレーク置換*)

set LAINBLAKEstr to do shell script "echo " & quoted form of str & " | perl -pe 's/
//g'"

log "★ラインブレーク置換str: " & LAINBLAKEstr & ""

(*★ラインブレーク置換str: 1LINE(LF)

2LINE(CR)

3LINE(LineBrake)3LINE_2*)

 

(*LFCR、ラインブレークをまとめて置換*)

set str to do shell script "echo " & quoted form of str & " | perl -pe 's/\\n|\\r|
//g'"

log "★改行置換str: " & str & ""

(*★改行置換str: 1LINE(LF)2LINE(CR)3LINE(LineBrake)3LINE_2*)

 


 

 

アップルスクリプトでの改行の取り扱いメモ

 

不可視文字の取り扱い

SPC  半角スペース  0x0020  英数+space  space  ASCII character (32)

  非改行スペース  0x00A0 英数+option+space  ASCII character (160)

  全角スペース  0x3000     かな+space

HT  タブ  0x0009  tab  tab  ASCII character (9)

ラインブレーク  0x2028  英数+control+return  行区切りで改行ではない

LF  パラグラフブレーク  0x000a  return  return / option+return  ASCII character (10) \\n

CR  キャリッジリターン  0x000d  option+control+return   ASCII character (13) \\r

FF  改ページ  0x000c  command+return  ASCII character (12)

改行を再認識

改行コード - Wikipedia

3/3 箇条書きで[Enter]と[Shift]+[Enter]の使い分け [ワード(Word)の使い方] All About

アップルスクリプトの改行に関するサイト

AppleScriptでの改行コードの扱い

それぞれの改行コードの10進数と16進数表示

10進数16進数文字
10 0x0aLF
130x0dCR
returnキーはLF、\nはLF、\rはCRだった。

アップルスクリプトの改行の置換に関するサイト

クリップボードに入っている情報の改行を外してクリップボードに書き戻す

try
  set a to the clipboard
  set b to a as string
  set c to repChar(b, ASCII character 10, "") of me
  set c to repChar(c, ASCII character 13, "") of me
  set the clipboard to c
end try
–文字置換ルーチン
on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  set temp to text items of origText
  set AppleScript’s text item delimiters to repStr
  set res to temp as text
  set AppleScript’s text item delimiters to txdl
  return res
end repChar

Apple Scriptの覚え書き

on replaceTxt(myText, searchTxt, replaceTxt) --置換専用
set oldDel to AppleScript's text item delimiters --デリミタを保存しておく
set AppleScript's text item delimiters to searchTxt --渡されたサーチワードをデリミタにする
set myText to every text item of myText --分解して
set AppleScript's text item delimiters to replaceTxt --渡された置換ワードをデリミタにする
set myText to myText as string --つなぐ
set AppleScript's text item delimiters to oldDel --デリミタを元に戻す
return myText
end replaceTxt
▲上のルーチンを使って改行コードを変換したりとか。文字列aaaを変換する例。
--まずは改行コードをセットして置いて
set cr to ASCII character(13)--CR
set lf to ASCII character(10)--LF
set crlf to cr & lf--CR+LF
set aaa to my replaceTxt(aaa, cr , lf) --CRをLFに変換/MAC→UNIX
set aaa to my replaceTxt(aaa, lf , cr) --LFをCRに変換/UNIX→MAC
set aaa to my replaceTxt(aaa, crlf , cr) --CR+LFをCRに変換/Windows→MAC
 
資料
[TIPs]不可視文字の入力: [FORCE]
鳶嶋工房 / AppleScript / Tips / 文字コード
ASCIIコード表
HTML Codes - Table of ascii characters and symbols
ASCII and Binary Characters (The Characters)