12. desktop picture設定 05.改訂版2013-10-05

05.スクリプトのバージョンアップ?です。
擬似的undo機能、複数画像選択時にはブラウズモード。
また画像ファイル選択時のみ動作するようにしました。
「メモ書き倉庫」さんから拝借したgetModifierKeysハンドラは内蔵仕様に。

拙作スクリプトは以下。アプリにされた後、お好みのアイコンを付けて、フォルダウィンドウのツールバーに登録して使う勝手が良いと思います。

property HomePict : ""
property PrevPict : ""

on setDTP(xxx)
tell application "System Events"
tell current desktop
if picture rotation = 1 then
set picture rotation to 0
end if
try
set PrevPict to picture as alias
end try
set picture to xxx as alias
end tell
end tell
end setDTP

on browseDTP(xxx)
tell application "System Events"
tell current desktop
if picture rotation = 1 then
set picture rotation to 0
end if
set currentpict to picture
repeat with pictitem in xxx
set picture to pictitem as alias
delay 4
end repeat
set picture to currentpict
end tell
end tell
end browseDTP

on getModifierKeys()
set theRubyScript to "require 'osx/cocoa';
event=OSX::CGEventCreate(nil);
mods=OSX::CGEventGetFlags(event);
print mods,' ';
print 'shift ' if (mods&0x00020000)!=0;

"
return do shell script "/usr/bin/ruby -e " & quoted form of theRubyScript
end getModifierKeys

tell application "Finder"
if my getModifierKeys() contains "shift" then
if (selection is not {}) then
set HomePict to selection as alias
set ppp to name of HomePict
display dialog "Home Picture set to " & ppp giving up after 2
my setDTP(HomePict)

else
set ppp to name of HomePict
display dialog "Back to Home Picture " & ppp giving up after 2
my setDTP(HomePict)
end if
else
if (selection is not {}) then
set tgtpicts to selection as alias list
if (count of tgtpicts) = 1 then
set tgtpict to selection as alias
--PDFなど追加あれば下記書き換えてください
if ((kind of tgtpict = "JPEG document") or (kind of tgtpict = "PNG document")) then
my setDTP(tgtpict)
else
return
end if
else

my browseDTP(tgtpicts)
end if
else
set ppp to name of PrevPict
display dialog "Back to Previous Picture " & ppp giving up after 2
my setDTP(PrevPict)

end if
end if
end tell

動作はスクリプトの内容順に、起動時に
・シフトキーを押しながら画像選択→Home Pictureとして設定
・シフトキーを押しながら何もファイル選択無し→Home Pictureへ復帰
・単純に画像選択→通常のdesktop pictureとして設定
・何もファイル選択無し→直前の設定画像へ戻る(undo)
・単純に複数画像選択→ブラウズモード
ブラウズモードの表示時間は、desktop pictureが換わる度にFinderがキャッシュを変更するので、あまり短くない方がいいでしょう。
フォルダウィンドウのツールバーからアプリ起動時に併用できるModifierKeyはshiftキーだけのようで、本作が目一杯という感じでしょうか。

11. 擬似的マルチ Picture Folder2013-04-30

選択フォルダ→リスト

自動でDesktop Pictureを変更される方向けの「Multiple pict foldersアプリ」です。
上図のように選択した画像フォルダのリストを作成、idle状態でDesktop Picture表示に使われるPicture Folderを自動変更します。Desktop Pictureが切り替わるタイミングと合わせて違和感を無くす、そんな狙いです。
ダウンロードは
https://dl.dropboxusercontent.com/u/164391356/Multiple%20pict%20folders.app.zip

このアプリの使い方

・Multiple pict foldersアプリはselection物なので、上図赤丸のようにFinder window toolbarや、Dockのようなランチャーに登録してください。
・Finder windowにて1個以上のフォルダ選択しながら当アプリを実行するとフォルダリストを作成します。windowでの見かけの並び順が反映します。選択は、commandキー、シフトキー、Select Allなどを駆使して好みのリストを作ってください。
・リストをForwardすると、画像またはフォルダのランダム表示を聞いてきますので、好みを決めれば設定完了で最初のPicture Folderへ切り替えます。以降、複数フォルダリストの場合は、idle状態でPicture Folderを変更していきます。フォルダが1個の場合、当アプリはDesktop Pictureを自動変更にして終了します。
・idle状態では、Tipsに述べる change intervalや「微調整」の変更ができます。また当アプリの終了は、idle状態のDock中アイコン右クリック経由がわかり易いでしょう。
・Desktop Picture自動変更中に当アプリを単に起動すると、自動変更停止します。またシフトキーを押しながら起動するとchange interval変更ができます。
・Finder上で何も選択しないで当アプリを起動すると、前回Forwardしたリストを返しますので、好みのフォルダリスト再実行が容易です。

Tips

・Picture Folderを自動変更するタイミングは(Desktop Pictureのchange interval + 微調整)です。Desktop Pictureのフェードアウトに合うよう微調整する訳です。
・idle状態の当アプリをシフトキー押しながらreopenするとchange interval、単にreopenすると微調整の変更ができます。0.1秒単位で有効です。微調整(スクリプトのproperty atime) の初期値0.2秒は作者の環境なので、個別に調整してください。新しい値が決まったらスクリプトのatimeを変更した方がいいでしょう。
・上図フォルダ内の「北極・南極 symlink」ファイルのような「フォルダのシンボリックリンク」を置けば選択有効です(ファイル数はカウントされませんが)。離れたフォルダも含めた理想のフォルダリストを目指しましょう。
・マックのヘルプのDesktop Picture項目にあるように、当アプリは描画関係アプリと相性が悪いのでご注意ください。単独使用でもたまに表示がズレますが、それは限界ということです(笑)。
・当アプリでも05. に書いた「メモ書き倉庫」さんのハンドラをありがたく使わせていただいています。

10. 不要なlprojフォルダを管理者権限で削除2013-04-28

最初に本項のスクリプトの注意書きから。
・使用するシェルスクリプトは「フォルダ=ディレクトリ、及びその中のファイルを完全に削除します」。ゴミ箱には入りませんので、誤って重要なファイルを消さぬよう留意して下さい。
・スクリプト内に「管理者パスワード」を記入しますので、「公の環境では絶対に用いない」など、情報管理に留意して下さい。


アプリケーションpackageなどのresourceフォルダ内に位置して多言語対応を提供するlprojフォルダ群について、その中から使わないので不要と定めた言語のものを削除できるのがMonolingualアプリです。
しかし、Monolingualアプリは一部System領域のpackageに及びませんので、検索アプリで調べると不要lprojフォルダが残る場合があります。「そこまで気になる方向け」に、手作業を要しますがMonolingualアプリ使用後の補助スクリプトです。

https://dl.dropboxusercontent.com/u/164391356/dellproj.scpt.zip

使い方はソフトウェアアップデートなどの後にまずMonolingualアプリを使用、その後で気になる不要lprojフォルダが残った場合にその情報をスクリプトへ追記して実行します。情報が累積すれば、追記の手間が無くなる訳です。
スクリプトの情報以外の期待しない削除は起こりませんが、こういった危険なスクリプトは「使用環境において仮のフォルダを使って動作を調べてから使う」など慎重に配慮してください。

09. Image Eventsで参照用画像作成2013-04-27

wallpaperフォルダ
上図のように、画像フォルダ群を納めた親フォルダの中に_Picture indexフォルダを作成、各画像フォルダの名前の参照用画像を収納します。
わたしのように「整理できない多くの画像フォルダの参照に困ったら」お助けになるかも、そんな用途です(笑)。
スクリプトは親フォルダ(上図でのwallpaperフォルダ)を選択しながら実行してください。なお_Picture indexフォルダが既に存在するとエラー起こしますので、再実行の場合は前に削除必要です。

tell application "Finder"

set rootfolder to selection as alias
set aList to every folder of (entire contents of rootfolder)

make new folder at rootfolder with properties {name:"_Picture index"}
set indexpath to (rootfolder as text) & "_Picture index:"
end tell

repeat with i from 1 to count of aList
set tgtFolder to item i of aList
repeat with h from 1 to count of tgtFolder
set www to item h in tgtFolder
if (name extension of www) = "jpg" then
exit repeat
else
set www to ""
end if

end repeat

if www is not "" then
set tgtFile to www as alias

tell application "Image Events"

set this_img to open tgtFile
set {_width, _height} to dimensions of this_img
if ((_width > 800) or (_height > 800)) then
scale this_img to size 800
end if

set ppp to name of tgtFolder as text
save this_img in file (indexpath & ppp & ".jpg") as JPEG
close this_img

end tell
end if
end repeat

tell application "Image Events" to quit


画像は各フォルダ内の名前順1枚目のjpgファイルを、縦横最長ピクセル固定に変換しています。変換対象や方法は色々工夫できますね。
AppleScriptのコンポーネントであるImage Eventsは地味な存在ですが、オプションが豊富なので画像ファイルを一括処理する用途にかなり役立つと思います。

08. Quick Look 的フォルダブラウズ2013-04-26

qlmanageウィンドウ
Quick Lookはファイルを手早くプレビューできて便利ですが、よくある画像ブラウザのような「フォルダを選択しているとその内容をブラウズできる」仕様ではありません。
残念ながらAppleScriptではQuick Lookは扱えませんが、そのデバッグ環境であるqlmanageならシェルスクリプトで可能。
という実験で組んだスクリプトなので、フォルダ1個かQuick Lookのようにファイル(複数可)を選択しながら実行してみてください。

tell application "Finder"
try
set tgtkind to kind of (selection as alias)

if tgtkind = "Folder" then
set theFolder to selection as alias
set these_items to (every item of theFolder) as alias list
set these_paths to ""
repeat with theAlias in these_items
set these_paths to these_paths & space & (the quoted form of the POSIX path of theAlias)
end repeat

else
set these_paths to (the quoted form of the POSIX path of (selection as alias))
end if

on error

set these_items to selection as alias list
set these_paths to ""
repeat with theAlias in these_items
set these_paths to these_paths & space & (the quoted form of the POSIX path of theAlias)

end repeat
end try
end tell

do shell script "qlmanage -p " & these_paths & " >& /dev/null"

結果は、上の画像のようなデバッグウィンドウで中のファイルをブラウズできます。
デバッグ環境なので一覧ウィンドウに切り替えると操作がまるで利かず終了するしかありませんが、Finder windowを閉じてもデバッグウィンドウは残るのが利点?でしょうか。
スクリプトとしては、error処理にてselectionがkindを持たないリスト、つまり複数項目選択の場合を担当しているのが面白いかと。
errorを活用できるケース、確かにあるんですね。