SSブログ

XcodeにApplescriptテンプレートを入れようシリーズ [AppleScript辞書はつくれるか?]

 image230328e.jpg

先日の記事(2023-03-27)にコメントをいただきまして、

アプリケーション内のテンプレートフォルダに直接入れるのではなく、ユーザーライブラリーに入れるのが、

最良ではないかということでしたので、考察してみました。

(参考サイト:http://piyocast.com/as/archives/15582

 

・アプリケーション側に入れる方法

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/macOS/Other/

このフォルダに入れると、アプリケーションを入れ直した際に元に戻ってしまうので、毎回入れ直さなければ

いけませんでした。(頻繁にバージョンアップしてるようですし)

 

・コメントでいただいた、ユーザーライブラリーに入れる方法

『(ユーザー)/Library/Developer/Xcode/』

このフォルダに入れると、ユーザーが変更されたり作り直しでもしない限りは、Xcodeがバージョンアップされても

反映されるようです。

 

ということで、反映するためのスクリプトを作ってみることにしました。

 

(* コピー元のアイテムを取得 *)

set add to ""

try

set ans to choose folder with prompt "使用する『AppleScript App.xctemplate』を選択してください" default location (path to (downloads folder))

if ans ≠ false then set add to ans as text

end try

if add = "" then

display alert "中止しました" message "ユーザーによってキャンセルされました" as critical

return "中止しました"

end if

tell application "Finder" to set aName to name of folder add

if aName ≠ "AppleScript App.xctemplate" then

display alert "対象外のアイテムが選ばれました" message "もう一度、正しいファイルを選択してください" as critical

return "対象外のアイテムが選ばれました"

end if

 

(* コピー先の選択 *)

set theList to {"システム・・・(全ユーザーに有効です)", "ユーザー・・・(現在のユーザーのみに適用します)", "Xcode.app・・・(現在のバージョンのみに適用します)"}

set ans to choose from list theList

if ans = false then

display alert "中止しました" message "ユーザーによってキャンセルされました" as critical

return "中止しました"

end if

set ans to ans as string

 

(* コピー先へアイテムを複製:保存先のフォルダも無ければ作成 *)

if (character 1 of ans) = "" then --●●●全ユーザー向け●●●

-- /Library/Developer/Xcodeフォルダの作成 --

set cd to "/Library/Developer"

try

do shell script ("cd " & cd & " ; ls 'Xcode' ;")

on error

do shell script ("cd " & cd & "  ; mkdir -m +w -p  Xcode ;") with administrator privileges

end try

-- /Library/Developer/Xcode/Templates/MacOS/UserTemplatesフォルダの作成 --

set cd to cd & "/Xcode"

repeat with obj in {"Templates", "MacOS", "UserTemplates"}

try

do shell script ("cd " & cd & " ; ls " & obj & " ;")

on error

do shell script ("cd " & cd & "  ; mkdir -m +w -p  " & obj & " ;")

end try

set cd to cd & "/" & obj

end repeat

tell application "Finder" to copy folder add to folder ((cd as POSIX file) as string)

 

else if (character 1 of ans) = "" then --●●●現在のユーザー向け●●●

-- (ユーザー)/Library/Developer/Xcodeフォルダの作成 --

set cd to "$HOME/Library/Developer"

try

do shell script ("cd " & cd & " ; ls 'Xcode' ;")

on error

do shell script ("cd " & cd & "  ; mkdir -m +w -p  Xcode ;") with administrator privileges

end try

-- (ユーザー)/Library/Developer/Xcode/Templates/MacOS/UserTemplatesフォルダの作成 --

set cd to cd & "/Xcode"

repeat with obj in {"Templates", "MacOS", "UserTemplates"}

try

do shell script ("cd " & cd & " ; ls " & obj & " ;")

on error

do shell script ("cd " & cd & "  ; mkdir -m +w -p  " & obj & " ;")

end try

set cd to cd & "/" & obj

end repeat

set currentFolder to do shell script ("echo " & cd & " ;") --ホームフォルダの変換

tell application "Finder" to copy folder add to folder ((currentFolder as POSIX file) as string)

 

else if (character 1 of ans) = "X" then --●●●個別アプリケーション向け●●●

-- 全てのアプリケーション情報を取得 --

set systemProfilerList to do shell script "system_profiler SPApplicationsDataType ;"

-- 上で取得した中からXcode.appのアドレスを抽出 --

set addressList to {}

repeat with oneLine in (paragraphs of systemProfilerList)

set oneLine to oneLine as string

if oneLine contains "Location:" then

if (oneLine ends with "/Xcode.app") or (oneLine ends with "/Xcode-beta.app") then

set n to offset in oneLine of ":"

set add1 to text (n + 2) thru -1 of oneLine

set addressList to addressList & {add1}

end if

end if

end repeat

if addressList = {} then return "Xcodeが見つけられませんでした。"

-- 選択 --

set ans to choose from list addressList with prompt "対象となるXcodeを選択してください" OK button name "選択" cancel button name "中止"

if ans = false then

display alert "見つかりませんでした" message "キャンセルされました" as critical

return "中止しました"

end if

-- テンプレートフォルダの作成--

set targetFolder to ""

tell application "Finder"

set folderAdd to folder (((ans as string) & "/Contents/Developer/Library/Xcode/Templates/Project Templates") as POSIX file as string)

set itemList to every folder of folderAdd

repeat with aFolder in itemList

if (name of aFolder) contains "mac" then

activate

try

select folder "Other:AppleScript App.xctemplate" of aFolder

display dialog "すでにテンプレートが存在します" buttons "OK" default button 1

return "すでにテンプレートが存在します"

end try

set cd to POSIX path of (aFolder as string)

try

do shell script ("cd \"" & cd & "\" ; ls UserTemplates ;")

on error

do shell script ("cd \"" & cd & "\"  ; mkdir -m +w -p  UserTemplates ;") with administrator privileges

end try

set targetFolder to ((cd & "/UserTemplates") as POSIX file) as string

end if

end repeat

end tell

if targetFolder ≠ "" then

-- テンプレートフォルダへ複製 --

try

tell application "Finder" to copy folder add to folder targetFolder

on error errText

display alert "続行不能" message errText as critical

return "エラーが発生"

end try

else

-- Xcodeがインストールされていないか不明のエラー --

display alert "対象が見つかりませんでした" message "キャンセルされました" as critical

return "中止しました"

end if

end if

 

activate

display dialog "AppleScriptテンプレートの準備ができました。" buttons "Ok" default button 1

"End"

 

 

※あらかじめAppleScript App.xctemplateは用意しておかなければなりません。

 

1)テンプレートアイテムを選択

image230328a.jpg

2)コピー先の選択

image230328b.jpg

『全ユーザー対象』『現在のユーザーのみ』『現在のアプリケーションのみ』の3つのモードを用意しました。

 

3)『現在のアプリケーションのみ』を選択した場合のみどのアプリケーションが対象なのかの選択ウインドウが

  出ます。

4)新たにテンプレートフォルダを作るので、1度だけユーザー認証のダイアログが出ますので認証してください。

5)準備ができましたのメッセージが出ましたら終了です。

 image230328c.jpg

作成されたフォルダー内にテンプレートがコピーされているはずです。

 image230328d.jpg

で、Xcodeを起動して新規作成画面でAppleScriptのテンプレートが表示されれば成功ですね。

       image230328e.jpg

 

いや、一度やってしまえばもう使わないだろう?...と思われるでしょうが、スクリプトを組みたいという趣味の部分

ですから、お気になさらず。

 


nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。