[pmset]電源の各種情報をplistに書き出して取得する(バッテリーシリアル、バッテリーヘルス、バッテリー最適化)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use scripting additions |
010 | |
011 | property refMe : a reference to current application |
012 | |
013 | ########################################## |
014 | # 【B-1】 pmsetコマンドでPLISTを作成する |
015 | log doMakePmsetPlist() |
016 | |
017 | ########################################## |
018 | # 【B-2】PLISTから値を取得する |
019 | ##バッテリーシリアル |
020 | log doGetPlistKeyPath("~/Documents/Apple/Battery/pmset_ps.plist", "Hardware Serial Number") as text |
021 | ##バッテリー状態 |
022 | log doGetPlistKeyPath("~/Documents/Apple/Battery/pmset_battery.plist", "BatteryHealth") as text |
023 | ##バッテリー最適化 |
024 | log doGetPlistKeyPath("~/Documents/Apple/Battery/pmset_battery.plist", "Optimized Battery Charging Engaged") as text |
025 | |
026 | |
027 | |
028 | ########################################## |
029 | # 【B-2】 パスとkeypath指定で値を取得する |
030 | to doGetPlistKeyPath(argFilePath, argKeyPath) |
031 | #パス |
032 | set strFilePath to argFilePath as text |
033 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
034 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
035 | set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
036 | ## |
037 | #レコードを読み込み |
038 | set listResponse to refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error| :(reference) |
039 | if (item 2 of listResponse) = (missing value) then |
040 | log "initWithContentsOfURL 正常処理" |
041 | set ocidPlistDict to (item 1 of listResponse) |
042 | else if (item 2 of listResponse) ≠ (missing value) then |
043 | log (item 2 of listResponse)'s code() as text |
044 | log (item 2 of listResponse)'s localizedDescription() as text |
045 | log "initWithContentsOfURL エラーしました" |
046 | return false |
047 | end if |
048 | # |
049 | set ocidValue to (ocidPlistDict's valueForKeyPath:(argKeyPath)) |
050 | return ocidValue |
051 | |
052 | end doGetPlistKeyPath |
053 | |
054 | ########################################## |
055 | # 【B-1】電源関係のPLISTを出力する |
056 | to doMakePmsetPlist() |
057 | #保存先を先に確保する |
058 | set appFileManager to refMe's NSFileManager's defaultManager() |
059 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
060 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
061 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/Battery") isDirectory:(true) |
062 | #フォルダを作る |
063 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
064 | # 777-->511 755-->493 700-->448 766-->502 |
065 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
066 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
067 | if (item 1 of listDone) is true then |
068 | log "createDirectoryAtURL 正常処理" |
069 | set ocidLabelNo to refMe's NSNumber's numberWithInteger:(2) |
070 | set listDone to (ocidSaveDirPathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference)) |
071 | if (item 1 of listDone) is true then |
072 | log "setResourceValue 正常処理" |
073 | else if (item 2 of listDone) ≠ (missing value) then |
074 | log (item 2 of listDone)'s code() as text |
075 | log (item 2 of listDone)'s localizedDescription() as text |
076 | return "setResourceValue エラーしました" |
077 | end if |
078 | else if (item 2 of listDone) ≠ (missing value) then |
079 | log (item 2 of listDone)'s code() as text |
080 | log (item 2 of listDone)'s localizedDescription() as text |
081 | log "createDirectoryAtURL エラーしました" |
082 | return false |
083 | end if |
084 | #ファイルを作成する |
085 | set ocidInfoFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("_このフォルダは削除しても大丈夫です.txt") isDirectory:(true) |
086 | set ocidInfoText to refMe's NSString's stringWithString:("このフォルダは削除しても大丈夫です") |
087 | set listDone to ocidInfoText's writeToURL:(ocidInfoFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
088 | if (item 1 of listDone) is true then |
089 | log "writeToURL 正常終了" |
090 | else if (item 1 of listDone) is false then |
091 | log (item 2 of listDone)'s localizedDescription() as text |
092 | log "writeToURL保存に失敗しました" |
093 | return false |
094 | end if |
095 | ################################# |
096 | #everything |
097 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("pmset_everything.txt") isDirectory:(false) |
098 | set strSaveFilePathURL to (ocidSaveFilePathURL's |path|()) as text |
099 | #コマンド実行 |
100 | set strCommandText to ("/bin/zsh -c '/usr/bin/pmset -g everything > \"" & strSaveFilePathURL & "\"'") as text |
101 | log strCommandText |
102 | try |
103 | do shell script strCommandText |
104 | on error |
105 | return false |
106 | end try |
107 | ################################# |
108 | #ps |
109 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("pmset_ps.plist") isDirectory:(false) |
110 | set strSaveFilePathURL to (ocidSaveFilePathURL's |path|()) as text |
111 | #コマンド実行 |
112 | set strCommandText to ("/bin/zsh -c '/usr/bin/pmset -g ps -xml > \"" & strSaveFilePathURL & "\"'") as text |
113 | log strCommandText |
114 | try |
115 | do shell script strCommandText |
116 | on error |
117 | return false |
118 | end try |
119 | ################################# |
120 | #batt |
121 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("pmset_battery.plist") isDirectory:(false) |
122 | set strSaveFilePathURL to (ocidSaveFilePathURL's |path|()) as text |
123 | #コマンド実行 |
124 | set strCommandText to ("/bin/zsh -c '/usr/bin/pmset -g batt -xml > \"" & strSaveFilePathURL & "\"'") as text |
125 | log strCommandText |
126 | try |
127 | do shell script strCommandText |
128 | on error |
129 | return false |
130 | end try |
131 | ################################# |
132 | #accps |
133 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("pmset_accps.plist") isDirectory:(false) |
134 | set strSaveFilePathURL to (ocidSaveFilePathURL's |path|()) as text |
135 | #コマンド実行 |
136 | set strCommandText to ("/bin/zsh -c '/usr/bin/pmset -g accps -xml > \"" & strSaveFilePathURL & "\"'") as text |
137 | log strCommandText |
138 | try |
139 | do shell script strCommandText |
140 | on error |
141 | return false |
142 | end try |
143 | return true |
144 | (* ■AllKeys |
145 | |
146 | BatteryHealthCondition |
147 | Power Source ID |
148 | Name |
149 | Max Capacity |
150 | Is Present |
151 | DesignCycleCount |
152 | Is Charged |
153 | version |
154 | BatteryHealth |
155 | Battery Provides Time Remaining |
156 | fccDaySampleCount |
157 | Maximum Capacity Percent |
158 | fccAvgHistoryCount |
159 | Current |
160 | Time to Empty |
161 | Battery Service Flags |
162 | Battery Service State |
163 | Transport Type |
164 | fccDaySampleAvgAlt |
165 | ncc |
166 | nccAvgAlt |
167 | nccAvg |
168 | fccDaySampleAvg |
169 | Power Source State |
170 | Time to Full Charge |
171 | nccAlt |
172 | Is Charging |
173 | Hardware Serial Number |
174 | Current Capacity |
175 | Optimized Battery Charging Engaged |
176 | LPM Active |
177 | Type |
178 | waitFc |
179 | *) |
180 | end doMakePmsetPlist |
AppleScriptで生成しました |
| 固定リンク