bgpict2.png 
わかばマークのMacの備忘録
初心者による Mac OSX の便利な使い方や、ソフトウェアの紹介など、Macのこといろいろ。

2023 / 06

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 



http://wakabamac.blog95.fc2.com/blog-entry-533.html

Quicksilver/ AppleScript で Action を作る(2)
icn_Quicksilver
Quicksilver : ランチャ+システムの一部のような総合ユーティリティ


Quicksilver は AppleScript を使って独自のActionを作ることができます。

前の記事『Quicksilver/ AppleScript で Action を作る(1)』ではテキストを扱うAppleScriptの話でしたが、今回はファイルを扱うものです。

ファイルを扱う場合のAppleScriptのひな形は、下のようになります。

▽ ScriptEditor で開く
on open of theFile
    --insert commands here:
end open

これの「--insert commands here:」のところにコマンドを入れれば、ファイルを扱うActionが作れます。
theFile」を違う名前にすると、機能しないみたいです。

icn_ScriptEditor
これをスクリプトエディタで「○○○.scpt」とか適当なファイル名で次の場所に保存します。

~/Library/Application Support/Quicksilver/Actions
(※「Actions」フォルダがない場合は作る)

これで Quicksilver を再起動すると、このスクリプトをActionとして使えるようになります。





【 QSのObjectを QuickLook で見るための Action 】


icn_QuickLook_scpt
以前の記事『Quicksilver/ QuickLookを表示』のActionもAppleScriptで作られています。

でもこのスクリプトだとちょっと不安定なので、
macosxhints.com - 10.5: Use Quick Look in Quicksilver
のコメントにあるものを使えば、安定します。

▽ ScriptEditor で開く
on open of theFile
    set thePath to quoted form of POSIX path of theFile
    do shell script "qlmanage -p " & thePath
    tell application Quicksilver to hide
end open



ただ、元は同じ動作をやっているので、一度開いた QuickLook のウインドウはマウスで「×」をクリックして閉じなければなりません。


そこでちょっと違うものを作ってみました。

▽ ScriptEditor で開く
on open of theFile
    tell application "Finder"
        activate
        reveal theFile
        tell application "System Events" to keystroke "y" using command down
    end tell
end open

これを「QuickLook (Reveal).scpt」とか適当な名前で

~/Library/Application Support/Quicksilver/Actions
(※「Actions」フォルダがない場合は作る)

の場所に保存して Quicksilver を再起動します。

これで1stPaneに表示したいものを設定して実行します。
080401qs9

すると、Finderで1stPaneに設定したファイルを選択・表示して QuickLook でそれを表示します。
080401qs10

これだと、スペースキーを押して閉じることができます。
Finderに表示される分、ちょっとウザイ気もするけど・・・。



※尚、このスクリプトは システム環境設定の「ユニバーサルアクセス」パネルで
補助装置にアクセスできるようにする」にチェックを入れないと機能しません。





【 QSのObjectを ゴミ箱に移動する Action 】


Quicksilver には元々「Move to Trash」Actionがあって、1stPaneのObjectをゴミ箱に移動することができます。
071023qs55


これでもいいんだけど、ゴミ箱に移動する前に確認ダイアログを出したいなと思ってAppleScriptで作ってみました。

▽ ScriptEditor で開く
-- Move to Trash [...]
on open of theFile
        
    set every_item to every item of theFile
    set count_item to count items of every_item
        
    set ret to return as text
    set qt to ASCII character 34
        
    if count_item = 1 then
        set thePath to POSIX path of theFile
        set theText to ret & ret & qt & (thePath as text) & qt & ret & ret
    else
        set theText to ret
    end if
        
    try
        tell application "Finder"
            set messeage to (count_item as string) & " 個のアイテム" & theText & "をゴミ箱 に移動しようとしています。" & ret & ret & ret & "本当によろしいですか?"
            display dialog messeage with icon caution
            delete every_item
        end tell
    end try
end open

これを「Move To Trash [...].scpt」とか適当な名前で
~/Library/Application Support/Quicksilver/Actions
に保存して、Quicksilver を再起動します。


これで1stPaneにいらないファイルを選択して、実行します。
080401qs11

そうすると、確認ダイアログが表示されます。
080401qs12

これだと誤ってファイルを捨ててしまう可能性が低くなると思います。

ついでに「Move to Trash」Actionでは使えなかった command+Z での undo も使えるようになります。





【 画像ファイルの画像をアイコンに貼付ける Action 】


JPG, PNG, TIFF画像ファイルの画像をアイコンに貼付けるActionです。
Leopard だとアイコンプレビューの機能があるので、いらないと言ったらいらないものだけど・・・。

これはデフォルトで入っているフォルダアクション用の「Image - Add Icon.scpt」のサンプルスクリプトを参考に作りました。

▽ ScriptEditor で開く
(* Image Add Icon *)
-- 画像ファイルの画像をアイコンに貼付ける Quicksilver の Action

-- コンバートするファイルタイプを設定
property type_list : {"JPEG", "TIFF", "PNGf"}

-- コンバートするファイル拡張子を設定
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png"}

on open of theFile
    try
        repeat with i from 1 to number of items in theFile
            set this_item to item i of theFile
            set item_info to info for this_item
                        
            if (alias of item_info is false and file type of item_info is in type_list) or (name extension of item_info is in the extension_list) then
                process_item(this_item)
            else
                tell application "Finder"
                    set error_file to name of this_item
                    set message to "This file was NOT converted :" & (return as text) & error_file
                    display dialog message buttons {"OK"} default button 1 giving up after 120 with icon caution
                end tell
            end if
        end repeat
                
        delay 0.5
        tell application "Quicksilver"
            open theFile
        end tell
                
    on error error_message number error_number
        if the error_number is not -128 then
            tell application "Finder"
                display dialog error_message buttons {"Cancel"} default button 1 giving up after 120 with icon stop
            end tell
        end if
    end try
        
end open


-- サブルーチン -- 画像ファイルの画像をアイコンに貼付ける 
on process_item(this_item)
    try
        set this_item to this_item as string
        with timeout of 900 seconds
            tell application "Image Events"
                launch -- always use with Folder Actions
                set this_image to open file this_item
                save this_image with icon
                close this_image
            end tell
        end timeout
    on error error_message
        tell application "Finder"
            display dialog error_message buttons {"Cancel"} default button 1 giving up after 120 with icon stop
        end tell
    end try
end process_item

このスクリプトを「Image Add Icon.scpt」とか適当な名前で
~/Library/Application Support/Quicksilver/Actions
に保存して、Quicksilver を再起動します。

そして1stPaneに JPG, PNG, TIFFの画像ファイル、2ndPaneに先程保存した「Image Add Icon」を選択して実行します。
080401qs13

すると、そのファイルアイコンに画像が貼付けられ、そのファイルをもう一度 Quicksilver の1stPaneに選択した状態でウインドウが開きます。
080401qs14
(画像が貼られたアイコンが、Quicksilver上ではなかなか反映されないこともあります。)



一度に複数の画像ファイルを選択しても大丈夫です。
080401qs15
(画像ファイルの入ったフォルダを指定することはできません。)







この記事では Quicksilver B55 を使用しています。


(関連記事)
Quicksilver/ AppleScript で Action を作る(1)
Quicksilver/ AppleScript で Action を作る(2)
Quicksilver/ AppleScript で Action を作る(3)

Quicksilver --- 基本的な概略説明
Quicksilver/ 基本的な使用例
Quicksilver/ Catalogの設定
Quicksilver/ Actionについて(1)
Quicksilver/ Actionについて(2)
Quicksilver/ Actionについて(3)
Quicksilver/ Actionについて(4)- Command Objects
Quicksilver/ Triggers の説明(1)
Quicksilver/ Triggers の説明(2)
Quicksilver/ Plug-ins について
Quicksilver/ Proxy Objects について
Quicksilver/ Triggers の説明(3)- Mouse Triggers
Quicksilver/ Triggers の説明(4)- Abracadabra Triggers
Quicksilver/ ショートカットまとめ

すべてのQuicksilver記事のリンクは『Quicksilver』の(関連記事)にあります。

edit

Quicksilver | CM:0 | TB:0

tag : Quicksilver  AppleScript 

+



管理者にだけ表示を許可する
 

trackback URL