AppleScriptを使用した怠zyな人のためのDockの折りたたみアプリケーション

グローバルホットキーをクリックしてアプリケーションウィンドウを表示し、フォーカスを失ったときにこのアプリケーションを非表示にできるプログラム(iTerm 2、Total Finder、Adium)のオプションをどのくらいの頻度で使用しますか? 個人的に、私は常にです。 しかし、特定のプログラムにそのような機能がなく、常に目の前にある場合はどうでしょうか? たとえば、同じSkype。 katomオプションの下で、ワークスペースを整理します。





ホットキーアプリケーションを開いて非表示にする



最も単純なものから始めましょう。 タスク:任意のプログラムからグローバルホットキーをクリックして、Skypeフォーカスを転送し、同じアクションで非表示にします。 非常に簡単にするプログラムが多数あります。 ただし、単純な方法を探しているわけではなく、これらの目的のために追加のプログラムをインストールする準備ができていません。 この場合、Automatorは、サービスを作成し、任意のアプリケーションからの呼び出しにホットキーを配置する機能を備えています。



Automatorを実行し、ドキュメントタイプServiceを選択します。 リストでは、 サービスは 、隣接するリストで入力データ 選択せずに 受け取り 、プログラムに残します。 AppleScript実行アクションをサービスに追加し、次のコードを貼り付けます。



set appName to "Skype" set startIt to false tell application "System Events" if not (exists process appName) then set startIt to true else if frontmost of process appName then set visible of process appName to false else set frontmost of process appName to true end if end tell if startIt then tell application appName to activate end if
      
      





それはどのように見えますか




このコードはここから正直に借用されています

ここではすべてが簡単です:スクリプトは、アプリケーションが実行されているかどうかを確認し、実行されていない場合は起動します。



サービスを保存し、左側のリストで[システム環境設定]> [キーボード]> [キーボードショートカット]に移動し、右側のリストで[ サービス]を選択して、作成したサービスを見つけ、それにホットキーをアタッチします。 目標は達成されましたが、目標がなければ、ここでは達成できません。



問題は、サービス呼び出しがグローバルホットキーにバインドされないことです。 サービスは、各アプリケーションから「ローカルに」呼び出されます。 たとえば、現在Safariを使用している間、プログラムメニューに[ サービス]サブメニュー(つまり、[ サファリ]> [サービス] )があり、ここからサービスを開始できます。 したがって、任意のアプリケーションから手動で起動でき、割り当てたホットキーの優先度は特定のアプリケーションの設定よりも低くなります。 これには2つの方法があります:システムの他の場所やプログラムで使用されていないサービスを呼び出すためのキーの組み合わせを割り当てるか、サービスを呼び出すためにグローバルホットキーをハングさせることを可能にするサードパーティプログラムの助けを借ります。 個人的には、3番目の方法で、Alfredの機能を利用しました。これにより、上記のアクションに頼らずに、特定のグローバルホットキーをクリックしてアプリケーションを表示および非表示にすることができます。



自動非表示はどうですか?



そのため、グローバルホットキーをクリックしてアプリケーションを表示および非表示にすることを学びましたが、別のアプリケーションに切り替える前にボタンを押してください。 この問題を解決します。



AppleScriptエディターを開き、次のコードを貼り付けます。



 property appName : "Skype" on idle tell application "System Events" set focusedApp to (name of the first process whose frontmost is true) if (focusedApp is not appName) and (exists process appName) and (visible of process appName) then set visible of process appName to false end if end tell return 0.5 end idle
      
      





[ ファイル]、[保存]の順にクリックし、ファイル形式プログラム )を選択し 、ハンドラーの開始後に[開いたままにする]の横にdawを置き、 プログラムを保存します。 その後、エディターで[ パッケージの内容 ]ボタンをクリックして使用できるようになり、右側にギアボタンを探しているウィンドウがポップアップ表示されます。 表示されるメニューで[ Finderに表示 ]を選択します。



それはどのように見えますか








Info.plistファイルは開かれたフォルダーにあり、テキストエディターで開き、4行目の後に挿入します。



 <key>LSBackgroundOnly</key> <string>1</string>
      
      







ここで何が起こるべきです
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>LSBackgroundOnly</key> <string>1</string> <key>CFBundleAllowMixedLocalizations</key> <true/> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleExecutable</key> <string>applet</string> <key>CFBundleIconFile</key> <string>applet</string> <key>CFBundleIdentifier</key> <string>com.apple.ScriptEditor.id.HideSkype</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>HideSkype</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleSignature</key> <string>aplt</string> <key>LSMinimumSystemVersionByArchitecture</key> <dict> <key>x86_64</key> <string>10.6</string> </dict> <key>LSRequiresCarbon</key> <true/> <key>WindowState</key> <dict> <key>dividerCollapsed</key> <false/> <key>eventLogLevel</key> <integer>2</integer> <key>name</key> <string>ScriptWindowState</string> <key>positionOfDivider</key> <real>333</real> <key>savedFrame</key> <string>55 281 602 597 0 0 1440 878 </string> <key>selectedTabView</key> <string>event log</string> </dict> </dict> </plist>
      
      







ファイルを保存します。



以上で、アプリケーションを実行し、自動ロードに追加して結果を楽しむことができます。 これからは、Skypeの目は冷淡ではありません。

実装機能に興味がある場合は、読み進めてください。



デブリーフィング



AppleScriptは私にとって発見であることがわかったので、ほんの2、3日前にはこの「奇跡」の存在を知りませんでした(主婦の言語のように見えます)。 ただし、作業環境のプロセスを自動化するのに十分な機会を提供します。 上記のアクションでは、開発者ツールをインストールする必要はありませんでした。その結果から、常に手元にある強力なツールの存在について話すことができます。



アプリケーションを自動終了する問題の解決策を整理してみます。



まず、特定のアプリケーションに焦点が合っているかどうかを判断します。



 set appName to "Skype" --,    tell application "System Events" --    application,      System Events set focusedApp to (name of the first process whose frontmost is true) --  focusedApp  ,       if focusedApp is appName then --  ,     appName    end if end tell
      
      





このようなコードを読むのは非常に珍しいことですが、文書化されたコードがあると便利です。



アプリケーションのフォーカスが失われた場合、非表示にします。



 set appName to "Skype" tell application "System Events" set focusedApp to (name of the first process whose frontmost is true) if (focusedApp is not appName) and (exists process appName) and (visible of process appName) then set visible of process appName to false end if end tell
      
      





これが問題の解決策です。 無限のサイクルでそれをラップするために残っています。



 set appName to "Skype" repeat while true tell application "System Events" set focusedApp to (name of the first process whose frontmost is true) if (focusedApp is not appName) and (exists process appName) and (visible of process appName) then set visible of process appName to false end if end tell delay 0.5 --        end repeat
      
      







そして、これは実用的なソリューションです。 ただし、この形式のままにすると、スクリプトは単にフリーズしますが、正しく機能します。 解決策は、ループを別のスレッドに入れることです。 しかし悲しいかな、AppleScriptにはスレッドがありません。 ドキュメントを見て、 アイドルハンドラーを見つけます。 AppleScriptハンドラーは、本質的に、私たちにとって馴染みのある手続きです。 アイドルハンドラの機能は、30秒ごとに呼び出されることです。値を返さない場合、たとえば5を返す場合、5秒ごとに呼び出されます。



 on idle set appName to "Skype" tell application "System Events" set focusedApp to (name of the first process whose frontmost is true) if (focusedApp is not appName) and (exists process appName) and (visible of process appName) then set visible of process appName to false end if end tell return 0.5 end idle
      
      







アイドルの使用を拒否し、 実行ハンドラと終了ハンドラを操作することもできます。



 property appName: "Skype" property running: true on run repeat while running tell application "System Events" set focusedApp to (name of the first process whose frontmost is true) if (focusedApp is not appName) and (exists process appName) and (visible of process appName) then set visible of process appName to false end if end tell delay 1 end repeat end run on quit set running to false end quit
      
      







複数のプログラムに1つのハンドラーが必要です



はい、問題ありません:



 property appsToHide: {"Skype", "Adium", "Sublime Text 2"} on idle tell application "System Events" set focusedApp to name of the first process whose frontmost is true repeat with appToHide in appsToHide if (focusedApp is not in appToHide) and (exists process appToHide) and (visible of process appToHide) then set visible of process appToHide to false end if end repeat end tell return 0.5 end idle
      
      







次の点に注意する価値がありますfocusedApp is not in appToHide



。 これは、 focusedApp



値がappToHide



focusedApp



れているかどうかを確認します。ただし、単に文字列を比較するのがappToHide



でしょう(演算子=, is equal, equals, [is] equal to, is not

isn't, isn't equal [to], is not equal [to], doesn't equal, does not equal




=, is equal, equals, [is] equal to, is not

isn't, isn't equal [to], is not equal [to], doesn't equal, does not equal




=, is equal, equals, [is] equal to, is not

isn't, isn't equal [to], is not equal [to], doesn't equal, does not equal




)。 ただし、この例では、文字列(データ型text



)を正しく比較したくありませんでした。



以上で私の話は終わりです。 AppleScriptに関する多くの詳細は、公式ドキュメントに記載れています 。 このツールの関連性に疑問がある場合は、 リリースノートをご覧ください。OSXのリリースごとに言語の機能が拡張されます。また、検索エンジンには、AppleScriptを使用した問題に対する既製のハウツーソリューションがたくさんあります。



ご清聴ありがとうございました。



All Articles