| AppleScript名:Macの製品カテゴリ名を取得する |
| — Created 2015-11-01 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set macRes to getSystemProfileInAGenre("SPHardwareDataType", "machine_name") of me –> "MacBook Pro" on getSystemProfileInAGenre(aTargGenre as string, aTargKey as string) set sRes to do shell script ("/usr/sbin/system_profiler -xml " & aTargGenre) set aSource to (readPlistFromStr(sRes) of me) as list set aaList to contents of first item of aSource set aList to _items of aaList repeat with i in aList set aDict to (current application’s NSMutableDictionary’s dictionaryWithDictionary:(contents of i)) set aKeyList to (aDict’s allKeys()) as list if aTargKey is in aKeyList then set aRes to (aDict’s valueForKeyPath:aTargKey) if aRes is not equal to missing value then return aRes as string end if end if end repeat return false end getSystemProfileInAGenre –stringのplistを読み込んでRecordに on readPlistFromStr(theString) set aSource to current application’s NSString’s stringWithString:theString set pListData to aSource’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aPlist to current application’s NSPropertyListSerialization’s propertyListFromData:pListData mutabilityOption:(current application’s NSPropertyListImmutable) |format|:(current application’s NSPropertyListFormat) errorDescription:(missing value) return aPlist end readPlistFromStr |
投稿者: Takaaki Naganoya
ソフトウェア的にアンマウントしたが物理的に接続解除していないHard Driveの名前を取得する v2
ソフトウェア的にアンマウントした状態で、各種接続端子(USBとか)で物理的につながったままの外部ドライブの名称を取得するAppleScriptです。
いちど、そういう質問が来て「できるわけがない」という回答をしていました。AppleScriptの性質上、ソフトウェア的にそういう操作は許可されていません。そもそも、そんな機能はありません。
ただ、まさかshell command経由でそうした操作ができるとは知りませんでした。冗談半分で調べてみたら存在したという状況です。
| AppleScript名:ソフトウェア的にアンマウントしたが物理的に接続解除していないHard Driveの名前を取得する v2 |
| — Created 2017-06-20 Shane Stanley — Modified 2017-06-20 Takaaki Naganoya use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set theResult to do shell script "diskutil list -plist" — make dictionary from property list set theResult to current application’s NSString’s stringWithString:theResult set pListDict to theResult’s propertyList() –> (NSDictionary) {AllDisks:{"disk0", "disk0s1", "disk0s2", "disk0s3", "disk1"}, AllDisksAndPartitions:{{DeviceIdentifier:"disk0", Size:500277790720, Content:"GUID_partition_scheme", Partitions:{{DiskUUID:"FC89778D-9028-4FAA-8118-F74802B0664E", DeviceIdentifier:"disk0s1", Size:209715200, Content:"EFI", VolumeName:"EFI", VolumeUUID:"85D67001-D93E-3687-A1C2-79D677F0C2E0"}, {DiskUUID:"AAF54C8C-9229-4DC1-8EA5-5FCCE9050DC4", DeviceIdentifier:"disk0s2", Size:499418034176, Content:"Apple_CoreStorage"}, {DiskUUID:"EE72FC16-C614-4C77-9048-6EF118451D48", DeviceIdentifier:"disk0s3", Size:650002432, Content:"Apple_Boot", VolumeName:"Recovery HD", VolumeUUID:"3400E36B-24AA-3B7A-93E3-BBDCDFF48548"}}}, {DeviceIdentifier:"disk1", Size:499055067136, MountPoint:"/", Content:"Apple_HFS", VolumeName:"Cherry"}}, WholeDisks:{"disk0", "disk1"}, VolumesFromDisks:{"Cherry"}} — extract relevant info set disksAndParitions to pListDict’s objectForKey:"AllDisksAndPartitions" –> (NSArray) {{DeviceIdentifier:"disk0", Size:500277790720, Content:"GUID_partition_scheme", Partitions:{{DiskUUID:"FC89778D-9028-4FAA-8118-F74802B0664E", DeviceIdentifier:"disk0s1", Size:209715200, Content:"EFI", VolumeName:"EFI", VolumeUUID:"85D67001-D93E-3687-A1C2-79D677F0C2E0"}, {DiskUUID:"AAF54C8C-9229-4DC1-8EA5-5FCCE9050DC4", DeviceIdentifier:"disk0s2", Size:499418034176, Content:"Apple_CoreStorage"}, {DiskUUID:"EE72FC16-C614-4C77-9048-6EF118451D48", DeviceIdentifier:"disk0s3", Size:650002432, Content:"Apple_Boot", VolumeName:"Recovery HD", VolumeUUID:"3400E36B-24AA-3B7A-93E3-BBDCDFF48548"}}}, {DeviceIdentifier:"disk1", Size:499055067136, MountPoint:"/", Content:"Apple_HFS", VolumeName:"Cherry"}} set partitionsArray to current application’s NSMutableArray’s array() — to store values repeat with anEntry in disksAndParitions set thePartitions to (anEntry’s objectForKey:"Partitions") if thePartitions = missing value then — no partitions means a volume (partitionsArray’s addObject:anEntry) else (partitionsArray’s addObjectsFromArray:thePartitions) end if end repeat — filter by Content type set thePred to current application’s NSPredicate’s predicateWithFormat:"Content == ’Apple_HFS’ OR Content == ’Apple_APFS’ " set partitionsArray to partitionsArray’s filteredArrayUsingPredicate:thePred — get names return (partitionsArray’s valueForKey:"VolumeName") as list |
指定のDiskの情報を取得する
| AppleScript名:指定のDiskの情報を取得する |
| — Created 2016-04-06 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set targDev to "/dev/disk3" –Blu-ray drive via USB set aDict to current application’s NSMutableDictionary’s alloc()’s init() try set a to do shell script "diskutil info " & targDev on error return false end try set aList to paragraphs of a repeat with i in aList set j to contents of i if j is not equal to "" then set jj to parseByDelim(j, ":") of me if (count every item of jj) is equal to 2 then copy jj to {j2, j3} set jj2 to (current application’s NSString’s stringWithString:j2) set jj3 to (current application’s NSString’s stringWithString:j3) set jjj2 to (jj2’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceCharacterSet())) set jjj3 to (jj3’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceCharacterSet())) (aDict’s setValue:jjj3 forKey:jjj2) end if end if end repeat return aDict as list of string or string –> {Read-Only Media:"Yes", OS Can Be Installed:"No", Volume Name:"Not applicable (no file system)", OS 9 Drivers:"No", Virtual:"No", Device Block Size:"2048 Bytes", Low Level Format:"Not supported", Removable Media:"Yes", Device / Media Name:"MATSHITA BD-MLT UJ240AS", Optical Media Erasable:"No", Optical Drive Type:"CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-R, DVD-R DL, DVD-RW, DVD-RAM, DVD+R, DVD+R DL, DVD+RW, BD-ROM, BD-R, BD-RE", Part of Whole:"disk3", Device Identifier:"disk3", Whole:"Yes", Optical Media Type:"BD-R", SMART Status:"Not Supported", Device Location:"External", Volume Free Space:"Not applicable (no file system)", Device Node:"/dev/disk3", Read-Only Volume:"Not applicable (no file system)", Media Type:"Generic", Total Size:"0 B (0 Bytes) (exactly 0 512-Byte-Units)", Media Removal:"Software-Activated", Mounted:"Not applicable (no file system)", Content (IOContent):"None", File System:"None", Protocol:"USB"} –テキストを指定デリミタでリスト化 on parseByDelim(aData, aDelim) set aText to current application’s NSString’s stringWithString:aData set aList to aText’s componentsSeparatedByString:aDelim return aList as list end parseByDelim |
Input Methodのメニュー表示名を取得する

| AppleScript名:Input Methodのメニュー表示名を取得する |
| — Created 2017-01-22 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "InputManager" –https://github.com/jensnockert/input-manager set cList to (current application’s CSInputSource’s all()’s valueForKey:"localizedName") as list –> {"ひらがな", "英字", "カタカナ", "日本語", "かなパレット", "com.apple.PressAndHold", "絵文字と記号", "キーボードビューア", "EmojiFunctionRowIM_Extension"}–10.10.x –> {"ひらがな", "英字", "カタカナ", "日本語", "かなパレット", "com.apple.PressAndHold", "絵文字と記号", "キーボードビューア"}–10.12.x |
InputManagerでIMを切り換える
InputManager.frameworkを呼び出して、日本語入力Input Methodの入力文字の切り替えを行うAppleScriptです。
スクリプトエディタ上でControl-Command-Rにてフロントエンド・プロセスで実行する必要があります。
| AppleScript名:InputManagerでIMを切り換える |
| — Created 2017-01-22 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "InputManager" –https://github.com/jensnockert/input-manager –Check If this script runs in foreground if not (current application’s NSThread’s isMainThread()) as boolean then display alert "This script must be run from the main thread (Command-Control-R in Script Editor)." buttons {"Cancel"} as critical error number -128 end if –Caution: This script runs on only Japanese language environment set cList to current application’s CSInputSource’s all() set dList to (cList’s valueForKey:"localizedName") set aRes to ((dList’s indexOfObject:"ひらがな") as integer) –"Hiragana" in Japanese set bRes to ((dList’s indexOfObject:"英字") as integer) –"English Letters" in Japanese set hiraKey to cList’s objectAtIndex:aRes set engKey to cList’s objectAtIndex:bRes repeat 10 times hiraKey’s |select|() delay 1 engKey’s |select|() delay 1 end repeat |
display情報の取得
| AppleScript名:display情報の取得 |
| — Created 2015-01-13 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" –Retina Displayを含む構成のとき(macOS 10.10.x) set dInfoList to retScreenInfos() –> {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69501832", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69513475", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69731202", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}} –Retina Displayを含む構成のとき(macOS 10.12.6) –> {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69731202", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}} –Retina Displayを含まない構成のとき(MacBook Pro Retina本体のLid Closed Mode時)(macOS 10.10.x) –set dInfoList to retScreenInfos() –> {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69501832", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69513475", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}} –Retina Displayを含まない構成のとき(MacBook Pro Retina本体のLid Closed Mode時)(macOS 10.12.6) –> {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69501831", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69513476", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}} on retScreenInfos() set sList to (current application’s NSScreen’s screens()) as list set dList to {} repeat with i in sList set a to i’s deviceDescription() set aSize to a’s NSDeviceSize as list set aResol to a’s NSDeviceResolution as list set aScrn to a’s NSDeviceIsScreen as boolean set aNum to a’s NSScreenNumber as string set aColSpc to a’s NSDeviceColorSpaceName as string set aColDepth to a’s NSDeviceBitsPerSample as string set the end of dList to {screenSize:aSize, screenResol:aResol, screenIsScreen:aScrn, screenNumber:aNum, screenColorSpace:aColSpc, screenColDepth:aColDepth} end repeat return dList end retScreenInfos |
Lock Screen
MacをLock Screenの状態にするAppleScriptです。
Lock Screenの状態では、実行中の他のAppleScriptはそのまま動作し続けます。音声出力はオフになります。
ただし、Lock Screen表示はそのまま表示され続けるため、画面表示をオフにしたいだけという用途には合っていません。
| AppleScript名:ASOCでLock Screen |
| — Created 2015-09-11 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aTask to current application’s NSTask’s alloc()’s init() aTask’s setLaunchPath:"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession" aTask’s setArguments:(current application’s NSArray’s arrayWithObject:"-suspend") aTask’s |launch|() |
画面情報を取得する
| AppleScript名:画面情報を取得する |
| use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions set allInfo to {} set theScreens to current application’s NSScreen’s screens() repeat with aScreen in theScreens set theInfo to aScreen’s deviceDescription() as record set theInfo to theInfo & aScreen’s frame() as record set end of allInfo to theInfo end repeat return allInfo –> {{NSDeviceResolution:{width:144.0, height:144.0}, NSDeviceSize:{width:1920.0, height:1200.0}, NSDeviceIsScreen:"YES", NSScreenNumber:2.077752445E+9, NSDeviceColorSpaceName:"NSCalibratedRGBColorSpace", NSDeviceBitsPerSample:8, origin:{x:0.0, y:0.0}, |size|:{width:1920.0, height:1200.0}}} –> {{NSDeviceResolution:{width:144.0, height:144.0}, NSDeviceSize:{width:1920.0, height:1200.0}, NSDeviceIsScreen:"YES", NSScreenNumber:69731202, NSDeviceColorSpaceName:"NSCalibratedRGBColorSpace", NSDeviceBitsPerSample:8, origin:{x:0.0, y:0.0}, |size|:{width:1920.0, height:1200.0}}, {NSDeviceResolution:{width:72.0, height:72.0}, NSDeviceSize:{width:1920.0, height:1080.0}, NSDeviceIsScreen:"YES", NSScreenNumber:458586661, NSDeviceColorSpaceName:"NSCalibratedRGBColorSpace", NSDeviceBitsPerSample:8, origin:{x:1920.0, y:216.0}, |size|:{width:1920.0, height:1080.0}}} |
使用メモリーの状況を取得
| AppleScript名:使用メモリーの状況を取得 |
| — Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set mRes to retMemoryUsage() of me –> {usedMem:7891, wiredMem:2220, unusedMem:293} on retMemoryUsage() set memRes to do shell script "top -l 1 | head -10 | grep ’PhysMem’" set aRes to (parseStrFromTo(memRes, " ", "M") of me) set bList to {} repeat with i in aRes set mRes to returnNumberOnly(i) of me set the end of bList to mRes end repeat set usedNum to contents of first item of bList set wiredNum to contents of second item of bList set unusedNum to contents of third item of bList return {usedMem:usedNum as integer, wiredMem:wiredNum as integer, unusedMem:unusedNum as integer} end retMemoryUsage on parseStrFromTo(aParamStr, fromStr, toStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set anArray to current application’s NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) — skip over separator theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference) if theValue is missing value then set theValue to "" –>追加 — skip over separator theScanner’s scanString:toStr intoString:(missing value) anArray’s addObject:theValue end repeat return anArray as list end parseStrFromTo on returnNumberOnly(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set anNSString to anNSString’s stringByReplacingOccurrencesOfString:"[^0-9]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()} return anNSString as text end returnNumberOnly |
電池容量(SPEC)を取得
| AppleScript名:電池容量(SPEC)を取得 |
| –バッテリー容量(Design Capacity) do shell script "ioreg -l | grep DesignCapacity | sed ’s/^.*DesignCapacity\" = \\([0-9]*\\)$/\\1/g’" |
バッテリ充放電の回数を取得する
| AppleScript名:バッテリ充放電の回数を取得する |
| –バッテリ充放電回数 set cycle to (do shell script "ioreg -l | grep ’\"CycleCount\"’ | sed ’s/^.*CycleCount. = \\([0-9]*\\)$/\\1/g’") |
macOS 10.14.6上で数値だけが返ってくるわけではなかったので、泥縄式にこんな風に書き換えて回数の数値だけえられるようにしてみました。
| AppleScript名:バッテリの充放電回数を取得する v2 |
| –バッテリ充放電回数 try set cycle to (do shell script "ioreg -l | grep ’\"CycleCount\"’ | sed ’s/^.*CycleCount. = \\([0-9]*\\)$/\\1/g’") set cRes to first item of (paragraphs of cycle) as number return cRes on error return false end try |
バッテリの容量を取得する
| AppleScript名:バッテリの容量を取得する |
| –バッテリ容量 set cap to (do shell script "ioreg -l | grep MaxCapacity | sed ’s/^.*MaxCapacity\" = \\([0-9]*\\)$/\\1/g’") as {number, anything} |
ACで動いているかDCで動いているかを取得する
| AppleScript名:ACで動いているかDCで動いているかを取得する |
| set curPowerStat to retACDC() of me
–ACで動作中かDCで動作中かを返す on retACDC() return word -2 of paragraph 1 of (do shell script "pmset -g cap") end retACDC |
ドライブマウント検出
ドライブのマウントを検出してsayコマンドで通知するAppleScriptです。
| AppleScript名:ドライブマウント検出 |
| — Created 2016-10-27 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" on run current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()’s addObserver:me selector:"onVolumeMount:" |name|:"NSWorkspaceDidMountNotification" object:(missing value) current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()’s addObserver:me selector:"onVolumeUnmount:" |name|:"NSWorkspaceDidUnmountNotification" object:(missing value) end run on onVolumeMount:aNotif say "マウントしました" using "Kyoko" –Mount Message In Japanese end onVolumeMount: on onVolumeUnmount:aNotif say "アンマウントしました" using "Kyoko" –Unmount Message In Japanese end onVolumeUnmount: |
TouchBarの検出
| AppleScript名:TouchBarの検出 |
| set aRes to detectTouchBar() of me –> true/false on detectTouchBar() tell application "System Events" set frontApp to first application process whose frontmost is true try set touchBar to first UI element of frontApp whose role is "AXFunctionRowTopLevelElement" on error errMsg number errNum return false end try set touchBarItems to value of attribute "AXChildren" of touchBar return (touchBarItems is not equal to {}) end tell end detectTouchBar |
USBに接続されたUSBメモリの情報を取得する v2
| AppleScript名:USBに接続されたUSBメモリの情報を取得する v2 |
set tmpPath to POSIX path of (path to temporary items from system domain) set aFileName to (do shell script "/usr/bin/uuidgen") set outPath to tmpPath & aFileName & ".plist" do shell script "/usr/sbin/system_profiler -xml SPUSBDataType > " & outPath tell application "System Events" set vRec to value of property list file (outPath as string) set v1Rec to _items of (first item of vRec) set dList to {} set sList to {} repeat with i in v1Rec set hitF to false try set j to _items of i set hitF to true end try if hitF = true then repeat with jj in j try set jjj to volumes of jj set sNum to d_serial_num of jj set vStr to b_vendor_id of jj repeat with ii in jjj set the end of dList to {serialNum:sNum, venderName:vStr, dData:contents of ii} end repeat end try end repeat end if end repeat end tell dList –> {{serialNum:"7f12db856195ef", venderName:"0x056e (Elecom Co., Ltd.)", dData:{mount_point:"/Volumes/NO NAME", _name:"NO NAME", writable:"yes", bsd_name:"disk2s1", free_space:"3.75 GB", file_system:"MS-DOS FAT32", |size|:"3.77 GB"}}, {serialNum:"M004101800001", venderName:"0x4146", dData:{mount_point:"/Volumes/ぴよまる", _name:"ぴよまる", writable:"yes", bsd_name:"disk1s9", free_space:"56 MB", file_system:"HFS+", |size|:"123.1 MB"}}} |
使用中のMacの製品呼称を取得する v3
| AppleScript名:使用中のMacの製品呼称を取得する v3 |
| use AppleScript version "2.5" use scripting additions use framework "Foundation" set mRes to retModelInfo() of me –> "MacBook Pro with Retina display, Intel Core i7, 15\" (Mid 2012)" on retModelInfo() set pListPath to "/System/Library/PrivateFrameworks/ServerInformation.framework/" & "Versions/A/Resources/English.lproj/SIMachineAttributes.plist" set aRec to retDictFromPlist(pListPath) of me set hwName to (do shell script "sysctl -n hw.model") –> "MacBookPro10,1" set aMachineRec to retRecordByLabel(aRec, hwName) set aMachineRec2 to contents of first item of aMachineRec return (marketingModel of _LOCALIZABLE_ of aMachineRec2) end retModelInfo –Read plist as record on retDictFromPlist(aPath) set thePath to current application’s NSString’s stringWithString:aPath set thePath to thePath’s stringByExpandingTildeInPath() set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath return theDict as record end retDictFromPlist –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) –ListからNSArrayへの型変換 set aArray to current application’s NSArray’s arrayWithArray:aRecList –抽出 set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate –NSArrayからListに型変換して返す return filteredArray as list end filterRecListByLabel –指定レコードの指定ラベルの値を取得する on retRecordByLabel(aRec as record, aKey as string) set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aRec set aVal to aDic’s valueForKey:aKey return aVal as list end retRecordByLabel –指定レコードの指定ラベルの値を取得する on retRecordByKeyPath(aRec as record, aKey as string) set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aRec set aVal to aDic’s valueForKeyPath:aKey return aVal end retRecordByKeyPath |
CPUのBrand Nameとバス速度を表示
| AppleScript名:CPUのBrand Nameとバス速度を表示 |
| set aTargLabel to "machdep.cpu.brand_string:" set aRes to retFilteredSysctrlValue(aTargLabel) of me –> "Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz" set bRes to retFilteredSysctrlValue("hw.busfrequency_max") of me –> " 100000000" return {aRes, bRes} –> {"Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz", " 100000000"} on retFilteredSysctrlValue(aTargLabel) set aRes to do shell script "sysctl -a | grep " & aTargLabel set aLen to length of aTargLabel set bRes to text (aLen + 2) thru -1 of aRes return bRes end retFilteredSysctrlValue |
使用CPUの状況を取得
| AppleScript名:使用CPUの状況を取得 |
| — Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aDict to retCpuUsage() of me –> {userCPU:3.2, sysCPU:12.29, idleCPU:84.49} on retCpuUsage() set cpuUseRes to do shell script "top -l 1 | head -10 | grep ’CPU usage’" set aRes to (parseStrFromTo(cpuUseRes, " ", "%") of me) set userUsage to (last item of (words of item 1 of aRes)) as real set sysUsage to (item 2 of aRes) as real set idleUsage to (item -2 of aRes) as real return {userCPU:userUsage, sysCPU:sysUsage, idleCPU:idleUsage} end retCpuUsage on parseStrFromTo(aParamStr, fromStr, toStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set anArray to current application’s NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) — skip over separator theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference) if theValue is missing value then set theValue to "" –>追加 — skip over separator theScanner’s scanString:toStr intoString:(missing value) anArray’s addObject:theValue end repeat return anArray as list end parseStrFromTo |
PFSystemKitでMac本体の情報を取得 v1.0
| AppleScript名:PFSystemKitでMac本体の情報を取得 v1.0 【Comment】 –https://github.com/perfaram/PFSystemKit |
| — Created 2016-07-26 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "PFSystemKit" set systemKit to current application’s PFSystemKit’s investigate() set a1 to systemKit’s platformReport()’s serial() as string –Hardware Serial –> "C0XXXXXXXXX2" set a2 to systemKit’s platformReport()’s model() as string –Hardware Model –> "MacBookPro10,1�" set a3 to systemKit’s platformReport()’s family() as integer –Hardware Family –> 5 set a4 to systemKit’s platformReport()’s uuid() as string –Hardware UUID –> "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" set a5 to systemKit’s platformReport()’s memorySize() as integer –RAM Size –> 8 set a6 to systemKit’s platformReport()’s boardID() as string –> "Mac-XXXXXXXXXXXXXXXX" set a7 to systemKit’s platformReport()’s romVersion() as string –> "MBP101.XXX.XXXX.XXX.XXXXXXXXXX" set a8 to systemKit’s platformReport()’s romReleaseDate() as date –> date 4017年8月8日火曜日 0:00:00 set a9 to systemKit’s platformReport()’s smcVersion() as string –SMC Version –> "2.3f36" set a10 to systemKit’s platformReport()’s sleepCause() as string –> "5" set a11 to systemKit’s platformReport()’s shutdownCause() as string –> "5" set a12 to systemKit’s platformReport()’s platform() as string –> "1" set a13 to systemKit’s platformReport()’s endianness() as string –> "0" |