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

このブログの人気の投稿

書字方向 横書方向変換スクリプト 左書きから右書きへ(コピペ用途)

PowerShellのGetDetailsOf メソッドでプロパティの詳細情報のID番号と項目名を列挙します

PowerShellで複数ファイルのプロパティを取得する方法(準備編)

簡単 YouTube動画をダウンロード、音声のみ保存する方法 2019

DOMノードオブジェクトを文字列に変換する

AppleScript 改行 コード 置換

PowerShellでJPG画像のリサイズとウォーターマーク画像との合成を同時に行う

Blender: 辺の長さを数値で指定するアドオン