Posted at

AppleScriptからCocoaの機能を使うためにフレームワークを取得する

More than 3 years have passed since last update.


  • AppleScriptでCocoaの機能を使える


    • できることが増えたり動作が高速化するメリット

    • 使うにはフレームワークのuse文が必要


      • 例:use framework "Foundation"





  • フレームワークを簡単に取得したい


    • 使えるフレームワークの場所


      • /System/Library/Frameworks/

      • /Library/Frameworks/

      • ~/Library/Frameworks/



    • 拡張子.frameworkとして存在



  • useFrameworks.scptはフレームワークを取得するAppleScript


    • フレームワークを選択


      • 一度に複数のフレームワークを選べる



    • use文をコピーまたは挿入


      • 挿入はスクリプトエディタを開いているときにだけ使える






useFrameworks.scpt

--framework選択

tell application "SystemUIServer"
activate
set chooseFrameworkResult to choose from list my getEveryFramework() with title "Framework" with multiple selections allowed
end tell
if chooseFrameworkResult = false then
error number -128
end if

--use文作成
set useFrameworkText to "use framework " & quote & my join(chooseFrameworkResult, quote & return & "use framework " & quote) & quote & return

--コピーまたは挿入
tell application "Script Editor"
if running and (exists documents) then
set dialogButtons to {"Cancel", "Insert", "Copy"}
else
set dialogButtons to {"Cancel", "Copy"}
end if
end tell
tell application "SystemUIServer"
activate
set {button returned:buttonResult} to display dialog "選択してください:" & return & return & useFrameworkText buttons dialogButtons default button 2 cancel button 1 with title "Framework"
end tell
if buttonResult = "Copy" then
set the clipboard to useFrameworkText
display notification useFrameworkText with title "Copied"
else
tell application "Script Editor"
activate
tell document 1
set text of it to useFrameworkText & text of it
try
check syntax
end try
end tell
end tell
end if

on getEveryFramework()
--framework取得
set frameworkNameList to {}
tell application "Finder"
repeat with libraryFolder in {path to library folder from system domain as alias, path to library folder from local domain as alias, path to library folder from user domain as alias}
if exists items of folder "Frameworks" of libraryFolder then
set frameworkNameList to frameworkNameList & name of items of folder "Frameworks" of libraryFolder
end if
end repeat
end tell
if frameworkNameList = {} then
error "Can't find any frameworks"
end if

--拡張子を外す
set textToRemoveExtension to my join(frameworkNameList, return) & return
return paragraphs 1 thru -2 of my replace(textToRemoveExtension, ".framework" & return, return)
end getEveryFramework

on join(textList as list, term as text)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to term
set aText to textList as string
set AppleScript's text item delimiters to oldDel
return aText
end join

on replace(str as text, find, replace as text)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set replaceList to text items of str
set AppleScript's text item delimiters to replace
set str to replaceList as string
set AppleScript's text item delimiters to oldDel
return str
end replace



  • useFrameworkForTextExpander.scptはTextExpander用のAppleScript



    • Content:アップルスクリプトとして登録

    • ファイルインのポップアップメニューでフレームワークを選択


      • 取得できるフレームワークはひとつだけだが動作が速い





useFrameworkForTextExpander.png


useFrameworkForTextExpander.scpt

set availableFrameworks to my getEveryFramework()

return "return " & quote & "use framework " & character id {92, 34, 37} & "fillpopup:name=framework:default=" & my join(availableFrameworks, ":") & character id {37, 92, 34, 34}

on getEveryFramework()
--framework取得
set frameworkNameList to {}
tell application "Finder"
repeat with libraryFolder in {path to library folder from system domain as alias, path to library folder from local domain as alias, path to library folder from user domain as alias}
if exists items of folder "Frameworks" of libraryFolder then
set frameworkNameList to frameworkNameList & name of items of folder "Frameworks" of libraryFolder
end if
end repeat
end tell
if frameworkNameList = {} then
error "Can't find any frameworks"
end if

--拡張子を外す
set textToRemoveExtension to my join(frameworkNameList, return) & return
return paragraphs 1 thru -2 of my replace(textToRemoveExtension, ".framework" & return, return)
end getEveryFramework

on join(textList as list, term as text)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to term
set aText to textList as string
set AppleScript's text item delimiters to oldDel
return aText
end join

on replace(str as text, find, replace as text)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set replaceList to text items of str
set AppleScript's text item delimiters to replace
set str to replaceList as string
set AppleScript's text item delimiters to oldDel
return str
end replace



更新履歴


  • 2016-01-28: フレームワークを取得するAppleScriptを作成

  • 2016-01-29: TextExpander用AppleScriptを作成