| AppleScript名:与えられた画像ファイルがNSImageでハンドリング可能かを取得 v2 | 
| — Created 2015-08-18 by Shane Stanley — Modified 2018-02-10 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" set oldPath to POSIX path of (choose file) set aRes to chkHandlingByNSImage(oldPath, "JPEG") –> {nsImageAcceptable:true, requireConvert:true} on chkHandlingByNSImage(aPath, targetFormatExt) set aRes to chkNSImageAcceptableFormat(aPath, targetFormatExt) set bRes to chkIsThereNeedToConvert(aPath, targetFormatExt) return {nsImageAcceptable:aRes, requireConvert:bRes} end chkHandlingByNSImage –与えられた画像ファイルがNSImageでハンドリング可能かを取得 on chkNSImageAcceptableFormat(aPath as text, targetFormatExt as text) — check if UTI of file is one NSImage can read set theWorkspace to current application’s NSWorkspace’s sharedWorkspace() set theType to theWorkspace’s typeOfFile:aPath |error|:(missing value) — returns UTI of file –> (NSString) "public.jpeg" set supportedTypes to current application’s NSImage’s imageTypes() — returns supported UTIs –> (NSArray) {"com.adobe.pdf", "com.apple.pict", "com.adobe.encapsulated-postscript", "public.jpeg", "public.png", "com.compuserve.gif", "public.jpeg-2000", "com.canon.tif-raw-image", "com.adobe.raw-image", "com.dxo.raw-image", "com.canon.cr2-raw-image", "com.leafamerica.raw-image", "com.hasselblad.fff-raw-image", "com.hasselblad.3fr-raw-image", "com.nikon.raw-image", "com.nikon.nrw-raw-image", "com.pentax.raw-image", "com.samsung.raw-image", "com.sony.raw-image", "com.sony.sr2-raw-image", "com.sony.arw-raw-image", "com.epson.raw-image", "com.kodak.raw-image", "public.tiff", "com.apple.icns", "com.canon.crw-raw-image", "com.fuji.raw-image", "com.panasonic.raw-image", "com.panasonic.rw2-raw-image", "com.leica.raw-image", "com.leica.rwl-raw-image", "com.konicaminolta.raw-image", "com.olympus.sr-raw-image", "com.olympus.or-raw-image", "com.olympus.raw-image", "com.adobe.photoshop-image", "com.microsoft.ico", "com.microsoft.bmp", "com.microsoft.cur", "com.truevision.tga-image", "com.sgi.sgi-image", "com.apple.macpaint-image", "com.ilm.openexr-image", "public.radiance", "public.mpo-image", "public.pbm", "public.pvr", "com.apple.rjpeg", "com.apple.quicktime-image", "com.kodak.flashpix-image"} if (supportedTypes’s containsObject:theType) as boolean is false then return "File format is unsupported" — check required type doesn’t already match else return true end if end chkNSImageAcceptableFormat –変換元の画像パスと変換対象の拡張子を与え、変換不要(同一ファイル"JPG"–> "JPEG"など)かをチェックする on chkIsThereNeedToConvert(aPath as text, targetFormatExt as text) set theWorkspace to current application’s NSWorkspace’s sharedWorkspace() set theType to theWorkspace’s typeOfFile:aPath |error|:(missing value) — returns UTI of file if (theWorkspace’s filenameExtension:targetFormatExt isValidForType:theType) as boolean then return false –"No conversion needed" else return true end if end chkIsThereNeedToConvert  | 
投稿者: Takaaki Naganoya
表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出
Safariで表示中のYouTubeのムービーをローカルのMoviesフォルダにダウンロードして音声部分のみ抽出するAppleScriptです。
YouTubeからのダウンロードを行う「YouTubeDLLib」およびShane StanleyのAppleScript Libraries「BridgePlus」を~/Libraries/AppleScript Librariesフォルダに、XAttribute.frameworkを~/Libraries/Frameworksフォルダにインストールして実行してください。
YouTubeDLLib内部には「youtube-dl」を含んでおり、これ単体でアップデートが必要な場合もあります。急に動かなくなったような場合には、新バージョンが出ていないかチェックしてみてください。
Script Menuから実行することを前提としています。
| AppleScript名:表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出 | 
| use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AVFoundation" use framework "XAttribute" –https://github.com/rylio/OTMXAttribute use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html use youtubeLib : script "YouTubeDLLib" set dlFolder to POSIX path of (path to movies folder) –ダウンロード先のフォルダ set aURL to getPageURLOfFrontmostWindow() of me set aList to aURL load framework –指定URLのYouTubeのムービーをダウンロードする set aRes to downLoadMovieToFile(aURL, dlFolder) of youtubeLib –オーディオ部分のみ抽出 set dlFullPath to dlFolder & aRes –ダウンロードしたMovieからXattrを削除する set xRes to removeXAttrFromFile(dlFullPath, "com.apple.quarantine") my convertMovieAt:dlFullPath ToType:"AVAssetExportPresetAppleM4A" withExtension:"m4a" deleteOriginal:true on getPageURLOfFrontmostWindow() tell application "Safari" if (count every window) = 0 then return false tell window 1 set aProp to properties end tell set aDoc to document of aProp set aText to URL of aDoc end tell return aText end getPageURLOfFrontmostWindow on getPageTitleOfFrontmostWindow() tell application "Safari" if (count every window) = 0 then return false tell window 1 set aProp to properties end tell set aDoc to document of aProp set aText to name of aDoc end tell return aText end getPageTitleOfFrontmostWindow –指定のムービーを、指定の書き出しプリセットで、指定のファイル形式で、オリジナルファイルを削除するかどうか指定しつつ書き出し on convertMovieAt:(posixPath as string) ToType:(assetTypeStr as string) withExtension:(aExt as string) deleteOriginal:(deleteFlag as boolean) set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath set aPreset to current application’s NSString’s stringWithString:assetTypeStr — set destination to use same path, different extension set destURL to theURL’s URLByDeletingPathExtension()’s URLByAppendingPathExtension:aExt set theAsset to current application’s AVAsset’s assetWithURL:theURL — check asset can be converted set allowedPresets to current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset if (allowedPresets’s containsObject:aPreset) as boolean is false then error "Can’t export this file as an ." & aExt & " file." end if — set up export session set fileTypeUTIstr to retFileFormatUTI(aExt) of me –ファイル拡張子からFile Type UTIを求める set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:aPreset theSession’s setOutputFileType:fileTypeUTIstr theSession’s setOutputURL:destURL — begin export and poll for completion theSession’s exportAsynchronouslyWithCompletionHandler:(missing value) repeat set theStatus to theSession’s status() as integer if theStatus < 3 then delay 0.2 else exit repeat end if end repeat — throw error if it failed if theStatus = (current application’s AVAssetExportSessionStatusFailed) as integer then error (theSession’s |error|()’s localizedDescription() as text) end if — delete original if required if deleteFlag then current application’s NSFileManager’s defaultManager()’s removeItemAtURL:theURL |error|:(missing value) end if end convertMovieAt:ToType:withExtension:deleteOriginal: on retFileFormatUTI(aExt as string) return (current application’s SMSForder’s UTIForExtension:aExt) end retFileFormatUTI on removeXAttrFromFile(aFile, anXattr) –Get Xattr String set anAttribute to (current application’s OTMXAttribute’s stringAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if anAttribute = missing value then return true –There is no use to remove xattr –Remove Xattr set xRes to (current application’s OTMXAttribute’s removeAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if xRes = missing value then return false return (xRes as boolean) end removeXAttrFromFile  | 
表示中のYouTubeのムービーをローカルにダウンロード v2
Safariで表示中のYouTubeのムービーをローカルのMoviesフォルダにダウンロードするAppleScriptです。
YouTubeからのダウンロードを行う「YouTubeDLLib」を~/Library/Script Librariesフォルダ(初期状態では存在しないので、ない場合には手動で作成)にインストールして実行してください。
–> Download YouTubeDLLib (To ~/Library/Script Libraries)
YouTubeDLLib内部には「youtube-dl」を含んでおり、これ単体でアップデートが必要な場合もあります(半年に1回ぐらいアップデートしています)。
Script Menuから実行することを前提としています。

| AppleScript名:表示中のYouTubeのムービーをローカルにダウンロード v2 | 
| use AppleScript version "2.4" use scripting additions use framework "Foundation" use youtubeLib : script "YouTubeDLLib" set dlFolder to POSIX path of (path to movies folder) –ダウンロード先のフォルダ set aURL to getPageURLOfFrontmostWindow() of me set aList to {aURL} set erList to {} –set aList to paragraphs of a repeat with i in aList set j to contents of i –指定URLのYouTubeのムービーをダウンロードする dlMovieSub(aURL, dlFolder) of youtubeLib end repeat return erList –ダウンロード結果 on getPageURLOfFrontmostWindow() tell application "Safari" if (count every window) = 0 then return false tell window 1 set aProp to properties end tell set aDoc to document of aProp set aText to URL of aDoc end tell return aText end getPageURLOfFrontmostWindow  | 
郵便専門ネットでXML-RPC経由で郵便番号を6桁(チェックデジット付き)の全国地方公共団体コード/JISコード/市町村コードに変換
| AppleScript名:郵便専門ネットでXML-RPC経由で郵便番号を6桁(チェックデジット付き)の全国地方公共団体コード/JISコード/市町村コードに変換 | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.postcodeToJiscode6", parameters:{"1760024"}} end tell –> "131202"  | 
郵便専門ネットでXML-RPC経由で引数に都道府県のJISコード(JISコードの先頭2文字)を渡すと、その都道府県に属しているJISコードを取得
| AppleScript名:郵便専門ネットでXML-RPC経由で引数に都道府県のJISコード(JISコードの先頭2文字)を渡すと、その都道府県に属しているJISコードを取得 | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.jiscodeFromPrefcode", parameters:{"13"}} end tell –> {"13101", "13102", "13103", "13104", "13105", "13106", "13107", "13108", "13109", "13110", "13111", "13112", "13113", "13114", "13115", "13116", "13117", "13118", "13119", "13120", "13121", "13122", "13123", "13201", "13202", "13203", "13204", "13205", "13206", "13207", "13208", "13209", "13210", "13211", "13212", "13213", "13214", "13215", "13218", "13219", "13220", "13221", "13222", "13223", "13224", "13225", "13227", "13228", "13229", "13303", "13305", "13307", "13308", "13361", "13362", "13363", "13364", "13381", "13382", "13401", "13402", "13421"}  | 
郵便専門ネットで一度に取得できるデータ件数を返す
| AppleScript名:郵便専門ネットで一度に取得できるデータ件数を返す | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.getMaxFetchCount", parameters:{}} end tell –> 100  | 
郵便専門ネットで郵便番号を5桁の全国地方公共団体コード/JISコード/市町村コードに変換
| AppleScript名:郵便専門ネットで郵便番号を5桁の全国地方公共団体コード/JISコード/市町村コードに変換 | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.postcodeExists", parameters:{"1760024"}} end tell –> true  | 
郵便専門ネットで郵便番号の存在チェック
| AppleScript名:郵便専門ネットで郵便番号の存在チェック | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.postcodeExists", parameters:{"1760024"}} end tell –> true  | 
郵便専門ネットで道府県のコード(地方公共団体コードの先頭2文字)から都道府県名を返す
| AppleScript名:郵便専門ネットで道府県のコード(地方公共団体コードの先頭2文字)から都道府県名を返す | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.getPrefName", parameters:{"13"}} end tell –> "東京都"  | 
郵便専門ネットで引数に指定した郵便番号で何件ヒットするのかをint型で返す
| AppleScript名:郵便専門ネットで引数に指定した郵便番号で何件ヒットするのかをint型で返す | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.getFetchCountByPostcode", parameters:{"1980036"}} end tell –> 1  | 
郵便専門ネットでXML-RPC経由で郵便番号から住所を返す
| AppleScript名:郵便専門ネットでXML-RPC経由で郵便番号から住所を返す | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.fetchAddressByPostcode", parameters:{"1760022"}} end tell –> {{other:application "http://yubin.senmon.net/service/xmlrpc/", addr_name:application "http://yubin.senmon.net/service/xmlrpc/", yid:176002201, postcode:"1760022", data_type:"p", addr_name_kana:application "http://yubin.senmon.net/service/xmlrpc/", city_kana:"ねりまく", jiscode:"13120", town:"向山", town_kana:"こうやま", city:"練馬区", pref:"東京都", pref_kana:"とうきょうと"}}  | 
郵便専門ネットでXML-RPC経由で郵便番号に対応する世界測地系(WGS84)の緯度経度コード(Geocode)を返す
| AppleScript名:郵便専門ネットでXML-RPC経由で郵便番号に対応する世界測地系(WGS84)の緯度経度コード(Geocode)を返す | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.postcodeToGeocode", parameters:{"1760024"}} end tell –> {35.0345563, 135.7476776}  | 
郵便専門ネットでXML-RPC経由でJISコード(5桁、6桁どちらでも)から、その市区町村に属している郵便番号のリストを取得
| AppleScript名:郵便専門ネットでXML-RPC経由でJISコード(5桁、6桁どちらでも)から、その市区町村に属している郵便番号のリストを取得 | 
| tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.postcodeFromJiscode", parameters:{"011037"}} end tell –> {"0070001", "0070002", "0070003", "0070004", "0070005", "0070006", "0070011", "0070030", "0070031", "0070032", "0070033", "0070034", "0070801", "0070802", "0070803", "0070804", "0070805", "0070806", "0070807", "0070808", "0070809", "0070810", "0070811", "0070812", "0070813", "0070814", "0070815", "0070819", "0070820", "0070821", "0070822", "0070823", "0070824", "0070825", "0070826", "0070827", "0070828", "0070829", "0070834", "0070835", "0070836", "0070837", "0070838", "0070839", "0070840", "0070841", "0070842", "0070843", "0070844", "0070845", "0070846", "0070847", "0070848", "0070849", "0070850", "0070851", "0070852", "0070861", "0070862", "0070863", "0070864", "0070865", "0070866", "0070867", "0070868", "0070869", "0070870", "0070871", "0070872", "0070873", "0070874", "0070880", "0070881", "0070882", "0070883", "0070884", "0070885", "0070886", "0070890", "0070891", "0070892", "0070893", "0070894", "0070895", "0078501", "0078503", "0078505", "0078507", "0078508", "0078553", "0078585", "0078632", "0600905", "0600906", "0600907", "0600908", "0600909", "0608569", "0608576", "0608582", "0650000", "0650004", "0650005", "0650006", "0650007", "0650008", "0650009", "0650010", "0650011", "0650012", "0650013", "0650014", "0650015", "0650016", "0650017", "0650018", "0650019", "0650020", "0650021", "0650022", "0650023", "0650024", "0650025", "0650026", "0650027", "0650028", "0650030", "0650031", "0650032", "0650033", "0650041", "0650042", "0650043", "0658501", "0658508", "0658510", "0658511", "0658518", "0658522", "0658533", "0658543", "0658550", "0658555", "0658558", "0658567", "0658578", "0658601", "0658609", "0658610", "0658611", "0658612", "0658633", "0658639"}  | 
郵便専門ネットでバージョン番号を取得
| AppleScript名:郵便専門ネットでバージョン番号を取得 | 
| –http://yubin.senmon.net/service/xmlrpc/ tell application "http://yubin.senmon.net/service/xmlrpc/" call xmlrpc {method name:"yubin.getVersion", parameters:{}} end tell –>"18.01a"  | 
ドメイン名からIPアドレスを取得
| AppleScript名:ドメイン名からIPアドレスを取得 | 
| — Created 2016-04-03 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set anIP to ((current application’s NSHost’s hostWithName:"www.google.com")’s address()) as string –> "216.58.200.196"  | 
インターネット接続確認
| AppleScript名:インターネット接続確認 | 
| — Created 2015-12-16 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRes to hasInternetConnection() of me –> true on hasInternetConnection() set aURL to current application’s |NSURL|’s alloc()’s initWithString:"http://www.google.com" set aReq to current application’s NSURLRequest’s alloc()’s initWithURL:aURL cachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:5.0 set urlRes to (current application’s NSURLConnection’s sendSynchronousRequest:aReq returningResponse:(missing value) |error|:(missing value)) if urlRes = missing value then return false else return true end if end hasInternetConnection  | 
Twitter投稿
macOS 10.13まではTwitter投稿やFacebook連携などもOSに組み込まれていたので、手軽にこうした機能を利用でき、macOS 10.13までの環境ではこのように呼び出せるようになっていました。
macOS 10.14から方針が変わり、これらの機能はOSに組み込まれなくなりました。いまだと、REST API経由で投稿を行うことになるでしょうか。


| AppleScript名:Twitter投稿 | 
| — Created 2015-01-24  by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aString to "ぴよ〜" set twService to current application’s NSSharingService’s sharingServiceNamed:(current application’s NSSharingServiceNamePostOnTwitter) set twService’s delegate to me set shareItems to current application’s NSMutableArray’s alloc()’s initWithObjects:aString tell twService to performWithItems:shareItems  | 
AirDropでファイルを送信する
Sharing Utilitiesを用いてAirDrop経由のファイル送信を行うAppleScriptです。
さすがに、Sharing Utilitiesは仕様も古いので、最新の環境に合わせて書き換えた「AirSharingLib Script Library」を配布、そちらを用いて同様の機能を実現するAppleScriptも掲載しています。
| AppleScript名:AirDropでファイルを送信する | 
| use AppleScript version "2.4" use scripting additions use framework "Foundation" use shareLib : script "Sharing Utilities" –https://macosxautomation.com/mavericks/libraries/examples.html set anItem to choose file with prompt "Select File to send via AirDrop" set aaList to {anItem} begin AirDrop with aaList  | 
LAN上のHDDをマウントする v2
| AppleScript名:LAN上のHDDをマウントする v2 | 
| –SMB経由でマウント set userAccount to "maro" –ユーザー名(環境に応じて書き換え) set userPass to "xxxxxx" –パスワード(環境に応じて書き換え) set aMachine to "smb://MBA11._smb._tcp.local/Macintosh HD" –11インチMacBook Air (10.9.2)でSMBのみ有効にしてテスト mountVolume(aMachine, userAccount, userPass) of me –通常実行 –AFP経由でマウント set userAccount to "maro" –ユーザー名(環境に応じて書き換え) set userPass to "xxxxxx" –パスワード(環境に応じて書き換え) set aMachine to "afp://MBA13._afpovertcp._tcp.local/Macintosh SSD" –13インチMacBook Air (10.7.5)でAFPとSMBの両方を有効にしてテスト mountVolumeAsync(aMachine, userAccount, userPass) of me –非同期実行 –指定のネットワーク上のディスクをマウントする on mountVolume(aMachine, userAccount, userPass) try with timeout of 100 seconds mount volume aMachine as user name userAccount with password userPass end timeout on error return false end try return true end mountVolume –指定のネットワーク上のディスクをマウントする(非同期実行) on mountVolumeAsync(aMachine, userAccount, userPass) try ignoring application responses mount volume aMachine as user name userAccount with password userPass end ignoring on error — end try end mountVolumeAsync  | 
無線LANデバイスのMACアドレスを取得する
| AppleScript名:無線LANデバイスのMACアドレスを取得する | 
| — Created 2015-08-18 by Takaaki Naganoya — Created 2015-08-18 by Shane Stanley — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "CoreWLAN" set aRes to retWiFiMACaddress() –> "XX:XX:XX:XX:XX:XX" –WiFi DeviceのMAC ADDRESSを返す on retWiFiMACaddress() –Get WLAN Device Name set allNames to current application’s CWInterface’s interfaceNames()’s allObjects() –> (NSArray) {"en0"} –@MacBook Pro Retina 2012 set hitF to false repeat with i from 1 to allNames’s |count|() set aInterface to (current application’s CWInterface’s interfaceWithName:(allNames’s objectAtIndex:(i – 1))) if aInterface’s serviceActive() as boolean then set hitF to true exit repeat end if end repeat if hitF = false then return false –There is No WiFi Device (Broken or illigal configuration or Hackintosh) set aHWAddress to aInterface’s hardwareAddress() as text –> "b8:xx:xx:xx:xx:xx"–MAC Address return aHWAddress end retWiFiMACaddress  |