[PlistBuddy]設定変更の流れ(ユーザー選択)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # 設定変更をユーザーが選択するパターンのサンプル |
005 | # PlistBuddyの場合 フルパスを指定する必要があり |
006 | # デバイスのUUIDを取得する処理が別途必要 |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "UniformTypeIdentifiers" |
011 | use framework "AppKit" |
012 | use scripting additions |
013 | |
014 | property refMe : a reference to current application |
015 | |
016 | ################## |
017 | #保存先確保 |
018 | set appFileManager to refMe's NSFileManager's defaultManager() |
019 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
020 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
021 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/IOreg") isDirectory:(true) |
022 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
023 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
024 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
025 | #保存パス |
026 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("IOPlatformExpertDevice.plist") isDirectory:(false) |
027 | set strSaveFilePath to ocidSaveFilePathURL's |path| as text |
028 | |
029 | ################## |
030 | #ioregの取得 plist保存 |
031 | # rd1=第一階層のみ c=プロパティのみ表示 |
032 | set strCommandText to ("/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice -a > \"" & strSaveFilePath & "\"") as text |
033 | log ("実行したコマンド\r" & strCommandText & "\r") as text |
034 | set strExecCommand to ("/bin/zsh -c \"" & strCommandText & "\"") as text |
035 | try |
036 | set strResponse to (do shell script strExecCommand) as text |
037 | on error |
038 | log "zshでエラーになりました\r" & strCommandText & "\r" |
039 | end try |
040 | |
041 | ################## |
042 | #値の取得 |
043 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Print:0:IOPlatformUUID\" \"" & strSaveFilePath & "\"") as text |
044 | log ("実行したコマンド\r" & strCommandText & "\r") as text |
045 | set strExecCommand to ("/bin/zsh -c \"" & strCommandText & "\"") as text |
046 | try |
047 | set strResponse to (do shell script strExecCommand) as text |
048 | on error |
049 | log "zshでエラーになりました\r" & strCommandText & "\r" |
050 | end try |
051 | |
052 | ################## |
053 | #ByHost設定ファイルの定義 |
054 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
055 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
056 | set ocidByHostDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Preferences/ByHost") isDirectory:(true) |
057 | set strByHostDirPath to ocidByHostDirPathURL's |path| as text |
058 | set strPlistFilePath to (strByHostDirPath & "/com.apple.Spotlight." & strResponse & ".plist") as text |
059 | |
060 | ################## |
061 | #現在の設定値の確認 |
062 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Print:MenuItemHidden\" \"" & strPlistFilePath & "\"") as text |
063 | log ("\r" & strCommandText & "\r") as text |
064 | set strExecCommand to ("/bin/zsh -c \"" & strCommandText & "\"") as text |
065 | try |
066 | set strResponse to (do shell script strExecCommand) as text |
067 | on error |
068 | log "zshでエラーになりました\r" & strCommandText & "\r" |
069 | end try |
070 | |
071 | ################## |
072 | #戻り値による分岐 |
073 | if strResponse is "true" then |
074 | set strMes to ("現在の設定はTRUE:1ですFALSE:0にしますか?") as text |
075 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:MenuItemHidden false\" \"" & strPlistFilePath & "\"") as text |
076 | else if strResponse is "false" then |
077 | set strMes to ("現在の設定はFALSE:0ですTRUE:1にしますか?") as text |
078 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:MenuItemHidden true\" \"" & strPlistFilePath & "\"") as text |
079 | end if |
080 | |
081 | ################## |
082 | #アラート選択 |
083 | try |
084 | #前面に |
085 | set strName to (name of current application) as text |
086 | if strName is "osascript" then |
087 | tell application "Finder" to activate |
088 | else |
089 | tell current application to activate |
090 | end if |
091 | set recordResponse to (display alert strMes buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" as informational giving up after 10) as record |
092 | on error |
093 | log "キャンセルしました。" |
094 | return "キャンセルしました。" |
095 | end try |
096 | if true is equal to (gave up of recordResponse) then |
097 | return "時間切れです。" |
098 | end if |
099 | #アラートの戻り値 |
100 | set strBottonName to (button returned of recordResponse) as text |
101 | |
102 | |
103 | ################## |
104 | #戻り値でコマンドを実行する |
105 | if "OK" is equal to (strBottonName) then |
106 | log ("\r" & strCommandText & "\r") as text |
107 | set strExecCommand to ("/bin/zsh -c '" & strCommandText & "'") as text |
108 | try |
109 | set strResponse to (do shell script strExecCommand) as text |
110 | on error |
111 | log "zshでエラーになりました\r" & strCommandText & "\r" |
112 | end try |
113 | end if |
114 | |
115 | ################## |
116 | #PlistBuddy Save |
117 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"" & strPlistFilePath & "\"") as text |
118 | log ("\r" & strCommandText & "\r") as text |
119 | set strExecCommand to ("/bin/zsh -c \"" & strCommandText & "\"") as text |
120 | try |
121 | set strResponse to (do shell script strExecCommand) as text |
122 | on error |
123 | log "zshでエラーになりました\r" & strCommandText & "\r" |
124 | end try |
125 | |
126 | ################## |
127 | |
128 | |
129 | return |
AppleScriptで生成しました |
| 固定リンク