AppleScript名:Bluetoothに接続中のデバイス名を取得、AirPodsを切断する |
— Created 2017-08-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" –macOS 10.11 or later use scripting additions use framework "Foundation" use framework "IOBluetooth" –参照 https://github.com/lapfelix/BluetoothConnector property IOBluetoothHostController : a reference to current application’s IOBluetoothHostController property IOBluetoothDevice : a reference to current application’s IOBluetoothDevice set dList to getActiveBluetoothDevices() of me –> {{deviceName:"Piyomaru AirPods", deviceAddress:"Xx-XX-xX-Xx-xx-xx"}, {deviceName:"Takaaki Naganoya のマウス", deviceAddress:"xx-xx-XX-xx-XX-Xx"}, {deviceName:"Takaaki Naganoya のキーボード #1", deviceAddress:"XX-XX-xX-xx-Xx-xX"}} set dRes to filterRecListByLabel(dList, "deviceName contains ’AirPods’") of me if dRes = {} then return false –Case: No match set dAddr to dRes’s first item’s deviceAddress set cnRes1 to disconnectBluetoothDeviceByAddress(dAddr) of me –> true (Successfully Disconnected) delay 10 set cnRes2 to connectBluetoothDeviceByAddress(dAddr) of me –> true (Successfully Connected) –指定アドレスのBluetooth Deviceを接続する on connectBluetoothDeviceByAddress(addressStr as string) if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass is equal to "IOBluetoothDevice" then set anAddress to i’s addressString() as string if anAddress = addressStr then i’s openConnection() return true end if end if end repeat return false end connectBluetoothDeviceByAddress –指定アドレスのBluetooth Deviceの接続を切る on disconnectBluetoothDeviceByAddress(addressStr as string) if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass is equal to "IOBluetoothDevice" then set anAddress to i’s addressString() as string if anAddress = addressStr then i’s closeConnection() return true end if end if end repeat return false end disconnectBluetoothDeviceByAddress –ペアリング済みのBluetooth Deviceの情報を取得 on getActiveBluetoothDevices() if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list set devList to {} repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass = "IOBluetoothDevice" then set aName to i’s |name|() as string set anAddress to i’s addressString() as string set aPaired to i’s isPaired() as boolean –set aConnect to i’s isConnected() as boolean if aPaired = true then set the end of devList to {deviceName:aName, deviceAddress:anAddress} end if end if end repeat return devList end getActiveBluetoothDevices –Mac本体のBluetoothのパワー状態を取得 on getBluetoothPowerState() set aCon to IOBluetoothHostController’s alloc()’s init() set pRes to (aCon’s powerState()) as boolean end getBluetoothPowerState –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) 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 set bList to filteredArray as list return bList end filterRecListByLabel |
タグ: 10.11savvy
Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出
AppleScript名:Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出 |
— Created 2017-08-06 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSPredicate : a reference to current application’s NSPredicate property NSMutableArray : a reference to current application’s NSMutableArray property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSPropertyListFormat : a reference to current application’s NSPropertyListFormat property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSPropertyListImmutable : a reference to current application’s NSPropertyListImmutable property NSPropertyListSerialization : a reference to current application’s NSPropertyListSerialization –製造者が"Apple"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral from "Apple" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-51, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}, {device_supportsESCO:"attrib_No", device_manufacturer:"Apple (0x3, 0x31C)", device_ispaired:"attrib_Yes", device_services:"Apple Wireless Mouse", device_isconnected:"attrib_No", device_majorClassOfDevice_string:"Peripheral", device_isNormallyConnectable:"attrib_Yes", device_isconfigured:"attrib_Yes", device_addr:"XX-XX-XX-XX-XX-XX", device_productID:"0x030D", device_supportsSSP:"attrib_No", device_vendorID:"0x05AC", device_classOfDevice:"0x05 0x20 0x2580", device_minorClassOfDevice_string:"Mouse", device_fw_version:"0x0084", device_supportsEDR:"attrib_No"}} –Appleのデバイスでも製造者がAppleになっていないものもある。Magic Keyboard 2とか –種類(マイナー)が "Headphones"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral about "Headphones" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-51, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}} –製造者が"Apple"で、種類(マイナー)が "Headphones"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral from "Apple" about "Headphones" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-52, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}} on returnBTPeripheral from devMaker as string : "" about kindName as string : "" set sRes to do shell script "/usr/sbin/system_profiler SPBluetoothDataType -detailLevel full -xml" set aSource to (readPlistFromStr(sRes) of me) as list set aaList to contents of first item of aSource set resArray to NSMutableArray’s new() set aList to _items of aaList repeat with i in aList set aDict to (NSMutableDictionary’s dictionaryWithDictionary:(contents of i)) set aKeyList to (aDict’s allKeys()) as list set dResList to (aDict’s valueForKeyPath:"device_title") repeat with ii in dResList set dKeyList to ii’s allKeys() set dKey to first item of dKeyList set dDic to (ii’s valueForKeyPath:dKey) if devMaker is not equal to "" and kindName is not equal to "" then set qText to "device_manufacturer contains ’" & devMaker & "’ && device_minorClassOfDevice_string ==’" & kindName & "’" else if devMaker is not equal to "" then set qText to "device_manufacturer contains ’" & devMaker & "’" else if kindName is not equal to "" then set qText to "device_minorClassOfDevice_string ==’" & kindName & "’" end if set dRes to filterRecListByLabel(dDic, qText) of me if (dRes as list) is not equal to {} then (resArray’s addObject:(first item of dRes)) end if end repeat end repeat return resArray as list end returnBTPeripheral –stringのplistを読み込んでRecordに on readPlistFromStr(theString) set aSource to NSString’s stringWithString:theString set pListData to aSource’s dataUsingEncoding:(NSUTF8StringEncoding) set aPlist to NSPropertyListSerialization’s propertyListFromData:pListData mutabilityOption:(NSPropertyListImmutable) |format|:(NSPropertyListFormat) errorDescription:(missing value) return aPlist end readPlistFromStr –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) set aArray to NSArray’s arrayWithArray:aRecList set aPredicate to NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel |
NSProcessInfoでプロセスの各種情報を取得
AppleScript名:NSProcessInfoでプロセスの各種情報を取得 |
— Created 2018-02-15 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aInfo to current application’s NSProcessInfo’s processInfo()’s processorCount() –> 8 set aInfo to current application’s NSProcessInfo’s processInfo()’s activeProcessorCount() –> 8 set aInfo to current application’s NSProcessInfo’s processInfo()’s physicalMemory() –> 8.589934592E+9 set aInfo to current application’s NSProcessInfo’s processInfo()’s systemUptime() –> 3.55849418142903E+5 set aInfo to (current application’s NSProcessInfo’s processInfo()’s hostName()) as string –> "mbpretina.local" set aInfo to (current application’s NSProcessInfo’s processInfo()’s operatingSystemVersionString()) as string –> "バージョン10.12.6(ビルド16G1309)" set vInfo to current application’s NSProcessInfo’s processInfo()’s operatingSystemVersion() –> {majorVersion:10, minorVersion:12, patchVersion:6} set aInfo to current application’s NSProcessInfo’s processInfo()’s isOperatingSystemAtLeastVersion:vInfo –> true set aInfo to (current application’s NSProcessInfo’s processInfo()’s thermalState()) –> 1 –0: NSProcessInfoThermalStateCritical –1: NSProcessInfoThermalStateFair –2: NSProcessInfoThermalStateNominal –3: NSProcessInfoThermalStateSerious |
デスクトップ・スクリーンセーバー
ステータスバーアイテムを作成してメニューバー上にメニューを出し、デスクトップ・スクリーンセーバーのコントロールを行うAppleScriptです。
ステータスバーアイテムを使って、簡単なメニュー操作を行うAppleScriptの試作品です。メニュー操作の方がメインで、デスクトップ・スクリーンセーバーの方はオマケです。
AppleScript名:デスクトップ・スクリーンセーバー |
— Created 2017-03-03 by Takaaki Naganoya — Modified 2018-02-15 by Shane Stanley–Thanks!! — Modified 2018-02-15 by Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property aStatusItem : missing value on run init() of me end run on init() set aList to {"Desktop ScreenSaver", "", "Stop", "", "Quit"} set aStatusItem to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength) aStatusItem’s setTitle:"💻" aStatusItem’s setHighlightMode:true aStatusItem’s setMenu:(createMenu(aList) of me) end init on createMenu(aList) set aMenu to current application’s NSMenu’s alloc()’s init() set aCount to 10 set prevMenuItem to "" repeat with i in aList set j to contents of i set aClass to (class of j) as string if j is equal to "" then set aMenuItem to (current application’s NSMenuItem’s separatorItem()) (aMenu’s addItem:aMenuItem) else if (aClass = "text") or (aClass = "string") then if j = "Quit" then set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") else set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") end if (aMenuItem’s setTag:aCount) (aMenuItem’s setTarget:me) (aMenu’s addItem:aMenuItem) set aCount to aCount + 10 copy aMenuItem to prevMenuItem else if aClass = "list" then –Generate Submenu set subMenu to current application’s NSMenu’s new() (aMenuItem’s setSubmenu:subMenu) set subCounter to 1 repeat with ii in j set jj to contents of ii set subMenuItem1 to (current application’s NSMenuItem’s alloc()’s initWithTitle:jj action:"actionHandler:" keyEquivalent:"") (subMenuItem1’s setTarget:me) (subMenuItem1’s setTag:(aCount + subCounter)) (subMenu’s addItem:subMenuItem1) set subCounter to subCounter + 1 end repeat end if end if end repeat return aMenu end createMenu on actionHandler:sender set aTag to tag of sender as string set aTitle to title of sender as string if aTitle is equal to "Quit" then current application’s NSStatusBar’s systemStatusBar()’s removeStatusItem:aStatusItem –tell me to quit else if aTag = "10" then do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &" else if aTag = "20" then killScreenSaver() of me set curMode to missing value end if end if end actionHandler: on killScreenSaver() set shellText to "killall ScreenSaverEngine" do shell script shellText end killScreenSaver |
soundIO Libで現在のサウンド入出力デバイス名を取得
現在設定されているサウンド入出力デバイス名を取得するAppleScriptです。
AppleScript名:soundIO Libで現在のサウンド入出力デバイス名を取得 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set i2 to soundIO’s getCurrentAudioInuptDevice() set o2 to soundIO’s getCurrentAudioOutuptDevice() return {i2, o2} –> {"Built-in Microphone", "Built-in Output"} |
soundIO Libでサウンド入出力を変更 v2.0
サウンドの入出力デバイスを任意のデバイスに変更するAppleScriptです。
–> soundIO Lib (To ~/Library/Script Libraries)
AppleScript名:soundIO Libでサウンド入出力を変更 v2.0 |
— Created 2016-10-07 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set outList to soundIO’s getEveryAudioOutputDevice() set targOutputDevice to contents of first item of (choose from list outList with prompt "Select Sound Output Device") set inList to soundIO’s getEveryAudioInputDevice() set targIntputDevice to contents of first item of (choose from list inList with prompt "Select Sound Intput Device") –入出力デバイスを設定 set i1 to soundIO’s setAudioInuptDevice(targIntputDevice) set o1 to soundIO’s setAudioOutuptDevice(targOutputDevice) set aRes to ({i1, o1} = {true, true}) return aRes |
soundIO Libでサウンド入出力を標準デバイスに設定
サウンドの入出力デバイスを標準デバイスに変更するAppleScriptです。
ただし、Mac miniではデフォルトのオーディオ入力デバイスが存在していないため、エラーになります。
AppleScript名:soundIO Libでサウンド入出力を標準デバイスに設定 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set targOutputDevice to "Built-in Output" set targIntputDevice to "Built-in Microphone" –Mac mini does not have default sound input device –出力デバイス一覧に設定対象が入っているかチェック set aList to soundIO’s getEveryAudioOutputDevice() if targOutputDevice is not in aList then return {false, "Target output device seems to not present"} –入力デバイス一覧に設定対象が入っているかチェック set bList to soundIO’s getEveryAudioInputDevice() if targIntputDevice is not in bList then return {false, "Target input device seems to not present"} –入出力デバイスを設定 set i1 to soundIO’s setAudioInuptDevice(targIntputDevice) set o1 to soundIO’s setAudioOutuptDevice(targOutputDevice) –サウンド入出力デバイスの変更確認 set i2 to soundIO’s getCurrentAudioInuptDevice() set o2 to soundIO’s getCurrentAudioOutuptDevice() set aRes to ({i1, o1} = {true, true}) and ({i2, o2} = {targIntputDevice, targOutputDevice}) return aRes |
soundIO Libでサウンド入出力をSoundFlowerに設定
サウンドの入出力デバイスをSoundFolwerに変更するAppleScriptです。
SoundFlowerをインストールしていない環境ではエラーになります。
AppleScript名:soundIO Libでサウンド入出力をSoundFlowerに設定 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set targDevice to "Soundflower (2ch)" set aList to soundIO’s getEveryAudioOutputDevice() if targDevice is not in aList then return false set bList to soundIO’s getEveryAudioInputDevice() if targDevice is not in bList then return false set i1 to soundIO’s setAudioInuptDevice(targDevice) set o1 to soundIO’s setAudioOutuptDevice(targDevice) set i2 to soundIO’s getCurrentAudioInuptDevice() set o2 to soundIO’s getCurrentAudioOutuptDevice() set aRes to (i1 = true and o1 = true) and (i2 = targDevice and o2 = targDevice) return aRes |
soundIO Libでサウンド入出力デバイス名一覧を取得
サウンドの入出力デバイス名の一覧を取得するAppleScriptです。
サウンドの入出力先を取得したり変更するのにシステム環境設定をGUI Scripting経由で操作している例をよく見かけますが、あまり上品なやり方ではないのでこのようなライブラリを利用することをお勧めします。
use soundIO : script “soundIO Lib” version “1.2” without importing
と、useコマンドでAppleScript Librariesを読み込む際に、オプションで「without importing」を指定しています。
これは、デフォルトの状態ではライブラリ本体の書き換えがすぐに反映されなかった(キャッシュされていた)ことに対処したものです。それほど頻繁にライブラリ側の書き換えを行わなければ、指定する必要はないでしょう。
AppleScript名:soundIO Libでサウンド入出力デバイス名一覧を取得 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set outList to soundIO’s getEveryAudioOutputDevice() –> {"Built-in Output", "Mobiola Headphone", "Mobiola Microphone", "Soundflower (2ch)", "Soundflower (64ch)"} set inList to soundIO’s getEveryAudioInputDevice() –> {"Built-in Microphone", "Mobiola Headphone", "Mobiola Microphone", "Soundflower (2ch)", "Soundflower (64ch)"} |
mpSoundEngineで電話の呼び出し音のような音
外部フレームワーク(mpSoundEngine)を利用して電話の呼び出し音のような音を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineで電話の呼び出し音のような音 |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine property engine : missing value set engine to current application’s MPMonoSoundEngine’s alloc()’s init() –https://pages.mtu.edu/~suits/notefreqs.html set soundList to {440.0, 466.16} repeat 10 times repeat with i in soundList makeSound(i) of me end repeat end repeat engine’s |stop|() on makeSound(aHz) engine’s channel()’s setFrequency:aHz engine’s start() delay 0.05 –engine’s |stop|() end makeSound |
mpSoundEngineで音階発生(Stereo)
外部フレームワーク(mpSoundEngine)を利用してステレオで(左右のチャンネルで別々の)音階の音を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineで音階発生(Stereo) |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine property engine : missing value set engine to current application’s MPStereoSoundEngine’s alloc()’s init() –https://pages.mtu.edu/~suits/notefreqs.html set soundList to {261.63, 293.66, 329.63, 349.23, 392.0, 440.0, 493.88, 523.25} set aLen to length of soundList repeat with i from 1 to aLen makeStereoSound(item i of soundList, item (aLen – i + 1) of soundList) of me end repeat engine’s |stop|() on makeStereoSound(aHz, bHz) engine’s leftChannel()’s setFrequency:aHz engine’s rightChannel()’s setFrequency:bHz engine’s start() delay 0.5 end makeStereoSound |
mpSoundEngineで音階を出す
外部フレームワーク(mpSoundEngine)を利用して音階の音を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineで音階を出す |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine –http://piyocast.com/as/archives/5030 set engine to current application’s MPMonoSoundEngine’s alloc()’s init() –https://pages.mtu.edu/~suits/notefreqs.html set soundList to {261.63, 293.66, 329.63, 349.23, 392.0, 440.0, 493.88, 523.25} engine’s start() repeat with i in soundList (engine’s channel()’s setFrequency:i) delay 0.1 end repeat engine’s |stop|() |
mpSoundEngineでループ音声発生(サイレン2)
外部フレームワーク(mpSoundEngine)を利用してループでサイレンのような音声を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineでループ音声発生(サイレン2) |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine –http://piyocast.com/as/archives/5030 set engine to current application’s MPMonoSoundEngine’s alloc()’s init() engine’s start() repeat with i from 1000 to 100 by -20 (engine’s channel()’s setFrequency:i) delay 0.01 end repeat engine’s |stop|() |
mpSoundEngineでループ音声発生(サイレン1)
外部フレームワーク(mpSoundEngine)を利用してループでサイレンのような音声を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineでループ音声発生(サイレン1) |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine –http://piyocast.com/as/archives/5030 set engine to current application’s MPMonoSoundEngine’s alloc()’s init() engine’s start() repeat with i from 100 to 1000 by 20 repeat with ii from i to 1000 by 20 (engine’s channel()’s setFrequency:ii) delay 0.005 end repeat end repeat engine’s |stop|() |
mpSoundEngineでランダム音発生(昔のSFでコンピュータが演算している風の音)
外部フレームワーク(mpSoundEngine)を利用して、ランダムに音(昔のSFでコンピュータが演算している風の音)を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineでランダム音発生(昔のSFでコンピュータが演算している風の音) |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine set engine to current application’s MPMonoSoundEngine’s alloc()’s init() engine’s start() repeat 100 times set aNum to random number from 50 to 4000 (engine’s channel()’s setFrequency:aNum) delay 0.02 end repeat engine’s |stop|() |
mpSoundEngineでA=440Hzのモノラルの音を出す
外部フレームワーク(mpSoundEngine)を利用して、440Hzの音を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineでA=440Hzのモノラルの音を出す |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine –http://piyocast.com/as/archives/5030 set engine to current application’s MPMonoSoundEngine’s alloc()’s init() engine’s channel()’s setFrequency:440.0 engine’s start() delay 3 engine’s |stop|() |
mpSoundEngineで8 octave分の音階発生
外部フレームワーク(mpSoundEngine)を利用して、8オクターブ分の音を鳴らすAppleScriptです。
AppleScript名:mpSoundEngineで8 octave分の音階発生 |
— Created 2017-12-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "mpSoundKit" –https://github.com/matthiasplappert/MPSoundEngine property engine : missing value –https://pages.mtu.edu/~suits/notefreqs.html set soundList to {16.35, 17.32, 18.35, 19.45, 20.6, 21.83, 23.12, 24.5, 25.96, 27.5, 29.14, 30.87, 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 46.25, 49.0, 51.91, 55.0, 58.27, 61.74, 65.41, 69.3, 73.42, 77.78, 82.41, 87.31, 92.5, 98.0, 103.83, 110.0, 116.54, 123.47, 130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185.0, 196.0, 207.65, 220.0, 233.08, 246.94, 261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.0, 415.3, 440.0, 466.16, 493.88, 523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61, 880.0, 932.33, 987.77, 1046.5, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 1567.98, 1661.22, 1760.0, 1864.66, 1975.53, 2093.0, 2217.46, 2349.32, 2489.02, 2637.02, 2793.83, 2959.96, 3135.96, 3322.44, 3520.0, 3729.31, 3951.07, 4186.01, 4434.92, 4698.63, 4978.03, 5274.04, 5587.65, 5919.91, 6271.93, 6644.88, 7040.0, 7458.62, 7902.13} set revList to reverse of soundList set engine to current application’s MPMonoSoundEngine’s alloc()’s init() engine’s start() repeat with i in soundList (engine’s channel()’s setFrequency:i) delay 0.01 end repeat repeat with i in revList (engine’s channel()’s setFrequency:i) delay 0.01 end repeat engine’s |stop|() |
ip-apiでIPアドレスから場所を検索
AppleScript名:ip-apiでIPアドレスから場所を検索 |
— Created 2017-06-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set myIP to getGeoLocationByIPAIP("45.59.69.202") of me –> {lon:-75.5281, zip:"19801", query:"45.59.69.202", as:"AS3800 Ionity Corporation", countryCode:"US", isp:"Ionity Corporation", lat:39.7157, city:"Wilmington", region:"DE", timezone:"America/New_York", org:"Ionity Corporation", country:"United States", regionName:"Delaware", status:"success"} –http://ip-api.com/docs/api:json on getGeoLocationByIPAIP(myIP) set aURL to "http://ip-api.com/json/" & myIP set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESTres to (json of aRes) set aRESTcode to responseCode of aRes if aRESTcode is not equal to 200 then return false return aRESTres as record end getGeoLocationByIPAIP –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept" set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as list of string or string set resHeaders to (dRes’s allHeaderFields()) as list of string or string else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults |
ipinfo.ioでIPアドレスから場所を検索
AppleScript名:ipinfo.ioでIPアドレスから場所を検索 |
— Created 2017-06-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set myIP to getGeoLocationByIPinfo("45.59.69.202") of me –> {region:"Delaware", city:"Wilmington", country:"US", org:"AS3800 Talent House, Inc.", hostname:"No Hostname", postal:"19801", ip:"45.59.69.202", loc:"39.7157,-75.5281"} –http://ipinfo.io/developers on getGeoLocationByIPinfo(myIP) set aURL to "http://ipinfo.io/" & myIP set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESTres to (json of aRes) set aRESTcode to responseCode of aRes if aRESTcode is not equal to 200 then return false return aRESTres as record end getGeoLocationByIPinfo –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept" set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as number set resHeaders to (dRes’s allHeaderFields()) as record else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults |
freegeoipで指定IPアドレスの位置情報を取得
AppleScript名:freegeoipで指定IPアドレスの位置情報を取得 |
— Created 2017-12-20 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" property |NSURL| : a reference to current application’s |NSURL| property NSData : a reference to current application’s NSData property NSJSONSerialization : a reference to current application’s NSJSONSerialization set aRes to getIPAddressInfoFreeGeoIP("91.108.183.75") of me as list of string or string –anything –> {time_zone:"Europe/Stockholm", city:"", zip_code:"", longitude:18.056, metro_code:0, country_name:"Sweden", latitude:59.3247, country_code:"SE", region_code:"", region_name:"", |ip|:"91.108.183.75"} –http://freegeoip.net on getIPAddressInfoFreeGeoIP(IPAddress) try with timeout of 10 seconds set link to "http://freegeoip.net/json/" & IPAddress set theURL to |NSURL|’s URLWithString:link set jsonData to NSData’s dataWithContentsOfURL:theURL set aJsonDict to (NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)) return aJsonDict end timeout on error return missing value end try end getIPAddressInfoFreeGeoIP |