AppleScript名:ムービー再生速度を変更する |
tell application "QuickTime Player" tell document 1 set rate to 2.0 end tell end tell |
タグ: 10.13savvy
QuickTime Playerの最前面のムービーの現在表示中のコマをデスクトップにJPEG画像で保存
AppleScript名:QuickTIme Playerの最前面のムービーの現在表示中のコマをデスクトップにJPEG画像で保存 |
— Created 2018-01-20 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" –http://piyocast.com/as/archives/5123 property NSUUID : a reference to current application’s NSUUID property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSPasteboard : a reference to current application’s NSPasteboard property NSJPEGFileType : a reference to current application’s NSJPEGFileType property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep set the clipboard to "" –Clear Clipboard (=Pasteboard) tell application "QuickTime Player" –No Document check if (count every document) < 1 then display dialog "No Document…" with icon 2 buttons {"OK"} default button 1 return end if –Audio Movie check if natural dimensions of front document = {0, 0} then display dialog "This is an audio file" with icon 2 buttons {"OK"} default button 1 return end if end tell repeat 100 times activate application "QuickTime Player" tell application "System Events" tell process "QuickTime Player" click menu item 5 of menu 1 of menu bar item 4 of menu bar 1 –Edit > Copy end tell end tell delay 0.1 set aRes to dataIsInClipboard for JPEG picture if aRes = true then exit repeat end repeat if aRes = false then return –クリップボードの内容をNSImageに set aNSIMage to my getClipboardASImage() –保存先とファイル名を求める set targFol to POSIX path of (path to desktop) set aUUID to NSUUID’s UUID()’s UUIDString() as text set outPath to targFol & aUUID & ".jpg" set aRes to saveNSImageAtPathAsJPG(aNSIMage, outPath, 1.0) of me — クリップボードの内容をNSImageとして取り出して返す on getClipboardASImage() set theNSPasteboard to NSPasteboard’s generalPasteboard() set imageArray to theNSPasteboard’s readObjectsForClasses:({NSImage}) options:(missing value) set targImage to imageArray’s objectAtIndex:0 return targImage end getClipboardASImage –NSImageを指定パスにJPEG形式で保存、qulityNumは0.0〜1.0。1.0は無圧縮 on saveNSImageAtPathAsJPG(anImage, outPath, qulityNum as real) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(NSJPEGFileType) |properties|:{NSImageCompressionFactor:qulityNum}) return (myNewImageData’s writeToFile:newPath atomically:true) as boolean end saveNSImageAtPathAsJPG –http://ashplanning.blogspot.jp/2006/12/blog-post.html on dataIsInClipboard for dataType return (clipboard info for dataType) is not {} end dataIsInClipboard |
Numbersでセルのカラム幅を自動調整 v2
AppleScript名:Numbersでセルのカラム幅を自動調整 v2 |
— Created 2018-1-11 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/5116 tell application "Numbers" tell front document tell sheet 1 set tCount to count every table if tCount = 0 then return tell table 1 set tableWidth to width set cCount to count every column set cWidth to width of every column set aWidth to width –table width set aveWidth to (aWidth – (first item of cWidth)) / (cCount – 1) tell columns 2 thru cCount set width to aveWidth end tell set wList to width of every column end tell if tCount = 1 then return set origCols to count every column of table 1 repeat with i from 2 to tCount if origCols is not equal to (count every column of table i) then display notification "Numbers: Column number is different in Table #" & (i as string) return end if end repeat repeat with i from 2 to tCount tell table i set width to tableWidth end tell set aCount to 1 tell table i repeat with ii from 1 to cCount tell column ii set width to (contents of item ii of wList) end tell end repeat end tell end repeat end tell end tell end tell |
Numbers書類からPDF書き出し v2
AppleScript名:Numbers書類からPDF書き出し v2 |
— Created 2017-03-28 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set tmpPath to (path to desktop) as string set aRes to exportNumbersDocToPDF(tmpPath) –Pages書類からPDF書き出し on exportNumbersDocToPDF(targFolderPath as string) tell application "Numbers" set dCount to count every document if dCount = 0 then return false end if set aPath to file of document 1 end tell set curPath to (current application’s NSString’s stringWithString:(POSIX path of aPath))’s lastPathComponent()’s stringByDeletingPathExtension()’s stringByAppendingString:".pdf" set outPath to (targFolderPath & curPath) tell application "Numbers" set anOpt to {class:export options, image quality:Best} export document 1 to file outPath as PDF with properties anOpt end tell end exportNumbersDocToPDF |
RGB色をHSBAに変換して色名称を計算 v2
AppleScript名:RGB色をHSBAに変換して色名称を計算 v2 |
— Created 2018-01-08 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSColor : a reference to current application’s NSColor set {rCol, gCol, bCol} to choose color set cName to retColorDomainName(rCol, gCol, bCol, 65535) of me on retColorDomainName(rCol as integer, gCol as integer, bCol as integer, aColorMax) set aCol to makeNSColorFromRGBAval(rCol, gCol, bCol, aColorMax, aColorMax) of me set hueVal to aCol’s hueComponent() set satVal to aCol’s saturationComponent() set brightVal to aCol’s brightnessComponent() if satVal ≤ 0.01 then set satVal to 0.0 set colName to "" if satVal = 0.0 then if brightVal ≤ 0.2 then set colName to "black" else if (brightVal > 0.95) then set colName to "white" else set colName to "gray" end if else if hueVal ≤ (15.0 / 360) or hueVal ≥ (330 / 360) then set colName to "red" else if hueVal ≤ (45.0 / 360) then set colName to "orange" else if hueVal < (70.0 / 360) then set colName to "yellow" else if hueVal < (150.0 / 360) then set colName to "green" else if hueVal < (190.0 / 360) then set colName to "cyan" else if (hueVal < 250.0 / 360.0) then set colName to "blue" else if (hueVal < 290.0 / 360.0) then set colName to "purple" else set colName to "magenta" end if end if return colName end retColorDomainName on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval |
16進数文字列とNumberの相互変換 v2.2
AppleScript名:16進数文字列とNumberの相互変換 v2.2 |
— Created 2018-01-20 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" repeat with i from 0 to 65535 set aRes to numToHexl(i) of hexKit set bRes to hextoNum(aRes) of hexKit if i ≠ bRes then display dialog "Not equal to " & (i as string) & " —> " & aRes & " —> " & bRes end repeat script hexKit property hexList : {missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, 10, 11, 12, 13, 14, 15} property stringSetList : {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"} –16進数文字列からNumberに変換 on hextoNum(hexText as string) set hList to characters of hexText set rhList to reverse of hList set digitNum to 0 set sumNum to 0 repeat with i in rhList set j to id of contents of i if j > 102 then return false set ofN to contents of item j of hexList of me if ofN = missing value then return false set sumNum to sumNum + (16 ^ digitNum) * (ofN) set digitNum to digitNum + 1 end repeat return sumNum as integer end hextoNum –数値を16進数文字列に変換 on numToHexl(origNum) if origNum = 0 then return "0" set resString to {} repeat if origNum ≤ 0 then exit repeat set resNum to (origNum mod 16) set resText to contents of item (resNum + 1) of stringSetList set resString to resText & resString set origNum to origNum div 16 end repeat return (resString as string) end numToHexl end script |
デスクトップ上のRetina解像度のスクリーンショット画像でICNS以外のものを検索
AppleScript名:デスクトップ上のRetina解像度のスクリーンショット画像でICNS以外のものを検索 |
— Created 2017-09-23 by Takaaki Naganoya — Modified 2017-12-01 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use mdLib : script "Metadata Lib" version "1.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib set theFolder to path to desktop set fRes to mdLib’s searchFolders:{theFolder} searchString:"kMDItemResolutionHeightDPI == %@ && kMDItemResolutionWidthDPI == %@ && kMDItemIsScreenCapture == %@ && NOT (kMDItemContentTypeTree contains %@)" searchArgs:{144, 144, true, "com.apple.icns"} –> {"/Users/me/Desktop/FromDesktop/スクリーンショット 2015-12-18 13.02.14.png", "/Users/me/Desktop/FromDesktop/スクリーンショット 2015-08-14 18.08.41.png"} |
指定日時以降に作成されたファイルをSpotlight検索 v3
AppleScript名:指定日時以降に作成されたファイルをSpotlight検索 v3 |
— Created 2017-09-21 by Takaaki Naganoya — Modified 2017-09-22 by Shane Stanley — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use mdLib : script "Metadata Lib" version "1.0.0" –http://piyocast.com/as/archives/4841 property NSTimeZone : a reference to current application’s NSTimeZone property NSCalendar : a reference to current application’s NSCalendar set aDate to getDateInternationalYMDhms(2017, 10, 22, 0, 0, 0) of me –> (NSDate) 2017-09-21 14:02:00 +0000 set thePath to POSIX path of (path to desktop) –> "/Users/me/Desktop/" set theFiles to mdLib’s searchFolders:{thePath} searchString:("kMDItemFSCreationDate >= %@") searchArgs:{aDate} –> returns POSIX path list on getDateInternationalYMDhms(aYear, aMonth, aDay, anHour, aMinute, aSecond) set theNSCalendar to NSCalendar’s currentCalendar() set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:aDay hour:anHour minute:aMinute |second|:aSecond nanosecond:0 return theDate end getDateInternationalYMDhms |
指定フォルダ内のラベル(Yellow, Red, Orange)のファイル一覧を取得
AppleScript名:指定フォルダ内のラベル(Yellow, Red, Orange)のファイル一覧を取得 |
— Created 2017-12-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use mdLib : script "Metadata Lib" version "1.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib set aLabelList to {5, 6, 7} –Yellow, Red, Orange –0: No Label, 1: Gray, 2: Green, 3: Purple, 4: Blue, 5: Yellow, 6: Red, 7: Orange set thePath to POSIX path of (path to desktop) set aRes to spotlightFindByLabels(aLabelList, thePath) of me –> list of POSIX path on spotlightFindByLabels(aLabelList as list, thePath as string) set aList to makeRepeatinglList(length of aLabelList, "kMDItemFSLabel == %@") set aStr to retStrFromArrayWithDelimiter(aList, " OR ") of me set fRes to mdLib’s searchFolders:{thePath} searchString:aStr searchArgs:aLabelList return fRes end spotlightFindByLabels –リストを指定デリミタをはさんでテキスト化 on retStrFromArrayWithDelimiter(aList as list, aDelim as string) set anArray to current application’s NSArray’s arrayWithArray:aList return (anArray’s componentsJoinedByString:aDelim) as string end retStrFromArrayWithDelimiter –指定回数、指定アイテムを連結したリストを作成 on makeRepeatinglList(hitNum as integer, hitItem as string) set outList to {} repeat hitNum times set the end of outList to hitItem end repeat return outList end makeRepeatinglList |
Maps.appで開始地点と到着地点の住所を指定して経路表示 v3
AppleScript名:Maps.appで開始地点と到着地点の住所を指定して経路表示 v3 |
— Created 2017-01-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aURL to "http://maps.apple.com/" set aRec to {saddr:"中村橋駅", daddr:"よみうりホール"} –経路表示 set bURL to retURLwithParams(aURL as string, aRec as record) open location bURL on retURLwithParams(aBaseURL as string, aRec as record) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set bLen to length of aValList if aLen is not equal to bLen then return false set qList to {} repeat with i from 1 to aLen set aName to (contents of item i of aKeyList) as string set aVal to (contents of item i of aValList) as string set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams |
com.apple.Maps
Maps.appで住所検索表示 v4
AppleScript名:Map.appで住所検索表示 v4 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aBaseURL to "http://maps.apple.com/" set aParam to "東京都港区六本木6丁目10番1号" –Apple Japanの住所 set aRec to {q:aParam} set cURL to retURLwithParams(aBaseURL, aRec) of me tell application "Maps" open location cURL end tell on retURLwithParams(aBaseURL as string, aRec as record) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set bLen to length of aValList if aLen is not equal to bLen then return false set qList to {} repeat with i from 1 to aLen set aName to (contents of item i of aKeyList) as string set aVal to (contents of item i of aValList) as string set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams |
com.apple.Maps
Maps.appで緯度経度指定表示 v3
AppleScript名:Map.appで緯度経度指定表示 v3 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aURL to "http://maps.apple.com/?ll=41.82459,140.7174504&t=m" –ラウンドワンスタジアム函館 open location aURL |
com.apple.Maps
iTunesでAirPlayデバイスを指定
AppleScript名:iTunesでAirPlayデバイスを指定 |
tell application "iTunes" set aList to properties of every AirPlay device –> {{class:AirPlay device, id:27, index:1, name:"コンピュータ", persistent ID:"0000000000000000", active:false, available:true, kind:computer, protected:false, selected:true, supports audio:true, supports video:true, sound volume:100}, {class:AirPlay device, id:58366, index:2, name:"Apple TV", persistent ID:"00005855CA413D01", active:false, available:true, kind:Apple TV, network address:"58:55:ca:41:3d:01", protected:false, selected:false, supports audio:true, supports video:true, sound volume:100}} set current AirPlay devices to AirPlay device "コンピュータ" end tell |
iTunesライブラリの曲のアーティスト名を集計
AppleScript名:iTunesライブラリの曲のアーティスト名を集計 |
— Created 2017-01-07 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "iTunesLibrary" set library to current application’s ITLibrary’s libraryWithAPIVersion:"1.0" |error|:(missing value) if library is equal to missing value then return set allTracks to library’s allMediaItems() set allCount to allTracks’s |count|() set anEnu to allTracks’s objectEnumerator() set newArray to current application’s NSMutableArray’s alloc()’s init() repeat set aPL to anEnu’s nextObject() if aPL = missing value then exit repeat try set aKind to (aPL’s mediaKind) as integer if (aKind as integer) is equal to 2 then –Music, Song set plName to aPL’s artist’s |name| as string set pl2Name to (my changeThis:" " toThat:"" inString:plName) –日本語アーティスト名で姓と名の間にスペースが入っているものがある(表記ゆらぎ)ので対策 newArray’s addObject:(pl2Name) end if on error set aLoc to (aPL’s location’s |path|()) as string log aLoc end try end repeat set aRes to countItemsByItsAppearance(newArray) of me –> {{theName:"浜田省吾", numberOfTimes:442}, {theName:"B’z", numberOfTimes:379}, {theName:"渡辺岳夫・松山祐士", numberOfTimes:199}, {theName:"VariousArtists", numberOfTimes:192}, {theName:"菅野よう子", numberOfTimes:108}, {theName:"布袋寅泰", numberOfTimes:100}, {theName:"三枝成彰", numberOfTimes:95}, {theName:"宇多田ヒカル", numberOfTimes:94}, {theName:"宮川泰", numberOfTimes:81}, {theName:"MichaelJackson", numberOfTimes:78}, {theName:"稲葉浩志", numberOfTimes:73}, … –出現回数で集計 on countItemsByItsAppearance(aList) set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList set bArray to current application’s NSMutableArray’s array() set theEnumerator to aSet’s objectEnumerator() repeat set aValue to theEnumerator’s nextObject() if aValue is missing value then exit repeat bArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (aSet’s countForObject:aValue)} forKeys:{"theName", "numberOfTimes"}) end repeat –出現回数(numberOfTimes)で降順ソート set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"numberOfTimes" ascending:false bArray’s sortUsingDescriptors:{theDesc} return bArray as list end countItemsByItsAppearance on changeThis:findString toThat:repString inString:someText set theString to current application’s NSString’s stringWithString:someText set theString to theString’s stringByReplacingOccurrencesOfString:findString withString:repString options:(current application’s NSRegularExpressionSearch) range:{location:0, |length|:length of someText} return theString as text end changeThis:toThat:inString: |
iTunesライブラリのファイル種別集計
AppleScript名:iTunesライブラリのファイル種別集計 |
— Created 2016-11-02 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "iTunesLibrary" –http://piyocast.com/as/archives/4301 set library to current application’s ITLibrary’s libraryWithAPIVersion:"1.0" |error|:(missing value) if library is equal to missing value then return set playLists to library’s allPlaylists() set gArray to library’s allMediaItems()’s |kind| set aRes to countItemsByItsAppearance(gArray) of me –> {{theName:"AAC オーディオファイル", numberOfTimes:3273}, {theName:"MPEG オーディオファイル", numberOfTimes:1768}, {theName:"購入した AAC オーディオファイル", numberOfTimes:1299}, {theName:"保護された AAC オーディオファイル", numberOfTimes:1102}, {theName:missing value, numberOfTimes:331}, {theName:"保護された MPEG-4 オーディオストリーム", numberOfTimes:183}, {theName:"MPEG オーディオストリーム", numberOfTimes:71}, {theName:"Apple ロスレス・オーディオファイル", numberOfTimes:36}, {theName:"QuickTime ムービーファイル", numberOfTimes:35}, {theName:"保護されたブック", numberOfTimes:28}, {theName:"AIFF オーディオファイル", numberOfTimes:21}, {theName:"MPEG-4 ビデオファイル", numberOfTimes:17}, {theName:"着信音", numberOfTimes:17}, {theName:"保護された MPEG-4 ビデオファイル", numberOfTimes:14}, {theName:"PDF 書類", numberOfTimes:11}, {theName:"ブック", numberOfTimes:8}, {theName:"購入したブック", numberOfTimes:6}, {theName:"iPhone/iPod touch/iPad App", numberOfTimes:5}, {theName:"購入した MPEG-4 ビデオファイル", numberOfTimes:5}, {theName:"インターネットオーディオストリーム", numberOfTimes:3}, {theName:"WAV オーディオファイル", numberOfTimes:2}, {theName:"iTunes Extras", numberOfTimes:2}, {theName:"iPhone/iPod touch App", numberOfTimes:1}, {theName:"QuickTime ムービー URL", numberOfTimes:1}, {theName:"Purchased AAC audio file", numberOfTimes:1}} –出現回数で集計 on countItemsByItsAppearance(aList) set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList set bArray to current application’s NSMutableArray’s array() set theEnumerator to aSet’s objectEnumerator() repeat set aValue to theEnumerator’s nextObject() if aValue is missing value then exit repeat bArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (aSet’s countForObject:aValue)} forKeys:{"theName", "numberOfTimes"}) end repeat –出現回数(numberOfTimes)で降順ソート set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"numberOfTimes" ascending:false bArray’s sortUsingDescriptors:{theDesc} return bArray as list end countItemsByItsAppearance |
iTunesライブラリのジャンル集計
AppleScript名:iTunesライブラリのジャンル集計 |
— Created 2016-11-02 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "iTunesLibrary" –http://piyocast.com/as/archives/4301 set library to current application’s ITLibrary’s libraryWithAPIVersion:"1.0" |error|:(missing value) if library is equal to missing value then return set playLists to library’s allPlaylists() set gArray to library’s allMediaItems()’s genre set aRes to countItemsByItsAppearance(gArray) of me –> {{theName:"サウンドトラック", numberOfTimes:1722}, {theName:"ロック", numberOfTimes:956}, {theName:"Podcast", numberOfTimes:721}, {theName:"クラシック", numberOfTimes:540}, {theName:"ポップ", numberOfTimes:529}, {theName:"J-Pop", numberOfTimes:382}, {theName:"アニメ", numberOfTimes:332}, {theName:missing value, numberOfTimes:287}, {theName:"Pop", numberOfTimes:279}, {theName:"社会/文化", numberOfTimes:279}, {theName:"World", numberOfTimes:218}, {theName:"ジャズ", numberOfTimes:188}, {theName:"Soundtrack", numberOfTimes:188}, {theName:"エレクトロニック", numberOfTimes:170}, {theName:"Classical", numberOfTimes:166}, {theName:"iTunes U", numberOfTimes:155}, {theName:"Rock", numberOfTimes:148}, {theName:"R&B", numberOfTimes:125}, {theName:"ニューエイジ", numberOfTimes:104}, {theName:"Unclassifiable", numberOfTimes:81}, {theName:"歌謡曲", numberOfTimes:58}, {theName:"オルタナティブ", numberOfTimes:58}, {theName:"Children’s", numberOfTimes:57}, {theName:"Holiday", numberOfTimes:38}, {theName:"Data", numberOfTimes:32}, {theName:"イージーリスニング", numberOfTimes:31}, {theName:"ワールド", numberOfTimes:30}, {theName:"ヴォーカル", numberOfTimes:29}, {theName:"ヒップホップ/ ラップ", numberOfTimes:24}, {theName:"Anime", numberOfTimes:23}, {theName:"フォーク", numberOfTimes:19}, {theName:"soundtrack", numberOfTimes:19}, {theName:"ブルース", numberOfTimes:15}, {theName:"ディズニー", numberOfTimes:15}, {theName:"シンガーソングライター", numberOfTimes:15}, {theName:"Easy Listening", numberOfTimes:14}, {theName:"ラテン", numberOfTimes:14}, {theName:"個人ジャーナル", numberOfTimes:14}, {theName:"Electronica/Dance", numberOfTimes:14}, {theName:"J-POP", numberOfTimes:9}, {theName:"New Age", numberOfTimes:9}, {theName:"アクション/アドベンチャー", numberOfTimes:9}, {theName:"ダンス", numberOfTimes:9}, {theName:"マンガ", numberOfTimes:7}, {theName:"演歌", numberOfTimes:7}, {theName:"プログラミング", numberOfTimes:7}, {theName:"Video", numberOfTimes:5}, {theName:"ホリデー", numberOfTimes:5}, {theName:"青年", numberOfTimes:5}, {theName:"キッズ/ファミリー", numberOfTimes:5}, {theName:"カントリー", numberOfTimes:4}, {theName:"R&B/ソウル", numberOfTimes:4}, {theName:"科学/医学", numberOfTimes:4}, {theName:"ビジネス", numberOfTimes:3}, {theName:"Latin", numberOfTimes:3}, {theName:"ヒップホップ/ラップ", numberOfTimes:3}, {theName:"チルドレン・ミュージック", numberOfTimes:3}, {theName:"#NIPPONSEI @ IRC.MIRCX.COM", numberOfTimes:2}, {theName:"コメディ", numberOfTimes:2}, {theName:"Technology", numberOfTimes:2}, {theName:"R&B/ソウル", numberOfTimes:2}, {theName:"ファンタジー", numberOfTimes:2}, {theName:"ユーティリティ", numberOfTimes:2}, {theName:"Vocal", numberOfTimes:2}, {theName:"ドラマ", numberOfTimes:2}, {theName:"社会科学", numberOfTimes:1}, {theName:"グラフィックノベル", numberOfTimes:1}, {theName:"コンピュータ/テクノロジー", numberOfTimes:1}, {theName:"伝記/自叙伝", numberOfTimes:1}, {theName:"女性", numberOfTimes:1}, {theName:"科学/自然", numberOfTimes:1}, {theName:"その他", numberOfTimes:1}, {theName:"児童書フィクション", numberOfTimes:1}, {theName:"業界/職業", numberOfTimes:1}, {theName:"レゲエ", numberOfTimes:1}, {theName:"Lifestyle & Home", numberOfTimes:1}, {theName:"マネジメント/リーダーシップ", numberOfTimes:1}, {theName:"スポークンワード", numberOfTimes:1}, {theName:"Dance", numberOfTimes:1}, {theName:"インストゥルメンタル", numberOfTimes:1}, {theName:"ライトノベル", numberOfTimes:1}, {theName:"SF/ファンタジー", numberOfTimes:1}, {theName:"ソーシャルネットワーキング", numberOfTimes:1}, {theName:"コンピュータ", numberOfTimes:1}, {theName:"146", numberOfTimes:1}, {theName:"健康/フィットネス", numberOfTimes:1}, {theName:"148", numberOfTimes:1}, {theName:"Electronic", numberOfTimes:1}, {theName:"仕事効率化", numberOfTimes:1}, {theName:"ライフスタイル", numberOfTimes:1}, {theName:"フィクション/文学", numberOfTimes:1}, {theName:"NHK FM(東京)", numberOfTimes:1}, {theName:"Seattle Pacific University – Latin", numberOfTimes:1}, {theName:"インスピレーショナル", numberOfTimes:1}, {theName:"日本/アジア", numberOfTimes:1}, {theName:"ブック", numberOfTimes:1}, {theName:"Kayokyoku", numberOfTimes:1}, {theName:"Folk", numberOfTimes:1}, {theName:"Romance", numberOfTimes:1}} –出現回数で集計 on countItemsByItsAppearance(aList) set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList set bArray to current application’s NSMutableArray’s array() set theEnumerator to aSet’s objectEnumerator() repeat set aValue to theEnumerator’s nextObject() if aValue is missing value then exit repeat bArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (aSet’s countForObject:aValue)} forKeys:{"theName", "numberOfTimes"}) end repeat –出現回数(numberOfTimes)で降順ソート set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"numberOfTimes" ascending:false bArray’s sortUsingDescriptors:{theDesc} return bArray as list end countItemsByItsAppearance |
Googleで検索して結果を返す v2
Webブラウザを併用せず、Googleの検索エンジンに直接問い合わせを行い、結果をCocoaの機能を用いて検索結果URLの一覧を取得するAppleScriptです。
オリジナル v1はShane Stanleyが作成、あまりに洗練された処理内容に絶句しました。さらに、処理内容が内容だけに、Shaneから見せられて「掲載はちょっと検討させて」と返事をしてしまったほど。
他のプログラミング言語でも同様の実装例が見られたので、問題視されないレベルと判断して掲載しました。
ただ、その一方でオリジナル版からの改良も行い、オリジナルでは20件ぐらいだった検索件数の拡張を行なって100件までの検索が行えるようにしたものがこのv2です。
長期間かつ大量の検索をGoogleの検索エンジンに対して行う場合には、REST API経由で行なってください。
追記(2019/08/20)
しばらく動いていましたが、Google側のWeb表示の仕様が変わって検索できなくなりました。ただし、検索結果のNSXMLDocumentからのURL抽出処理を変更することで、対応は可能と思われます(やっていませんが)。REST APIのドキュメント化された仕様は変わらないため、検索を行うにはREST API経由のほうが安心です。
追記(2020)
CotEditorのPowerPackにこれの改訂版を入れてあり、CotEditorで選択中の語句でGoogle検索できるようにしてあります。これの動作状況で様子を見ているところです。
AppleScript名:Googleで検索して結果を返す v4 |
— Created 2017-03-31 by Shane Stanley — Modified 2019-09-18 by Takaaki Naganoya use AppleScript version "2.4" use framework "Foundation" use scripting additions property theResult : {} set theQuery to "戦場の絆" set paramObj to {myQuery:theQuery, myMax:10} my performSelectorOnMainThread:"searchByGoogle:" withObject:(paramObj) waitUntilDone:true return my theResult –> {"http://web.gundam-kizuna.jp/sp/",…….} on searchByGoogle:paramObj set theQuery to myQuery of paramObj set maxNum to myMax of paramObj — build and escape query string set theQuery to current application’s NSString’s stringWithString:theQuery set theQuery to theQuery’s stringByReplacingOccurrencesOfString:space withString:"+" set escQuery to theQuery’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet()) — do search set searchURLString to current application’s NSString’s stringWithFormat_("https://www.google.com/search?client=safari&rls=en&ie=UTF-8&oe=UTF-8&q=%@&complete=0&num=%@&as_qdr=all", escQuery, (maxNum as string)) set theURL to current application’s |NSURL|’s URLWithString:searchURLString set {theData, theError} to current application’s NSData’s dataWithContentsOfURL:theURL options:0 |error|:(reference) if theData = missing value then error (theError’s localizedDescription() as text) — convert to XML set {theXMLDoc, theError} to current application’s NSXMLDocument’s alloc()’s initWithData:theData options:(current application’s NSXMLDocumentTidyHTML) |error|:(reference) if theXMLDoc = missing value then error (theError’s localizedDescription() as text) –検索結果として返してくるclass要素名を固定で書かない方法を模索するべき? — filter via XPath and predicate set xpathStr to "//*[@class=\"kCrYT\"]/a/@href" –ここが問題 set {theNodes, theError} to theXMLDoc’s nodesForXPath:xpathStr |error|:(reference) if theNodes = missing value then error (theError’s localizedDescription() as text) set nodeStrings to (theNodes’s valueForKey:"stringValue") set thePred to current application’s NSPredicate’s predicateWithFormat:"self BEGINSWITH ’/url?q=’" set nodeStrings to nodeStrings’s filteredArrayUsingPredicate:thePred — trim URL strings set theURLStrings to {} repeat with aString in nodeStrings set theOffset to (aString’s rangeOfString:"&") set end of theURLStrings to (aString’s substringWithRange:{7, (theOffset’s location) – 7}) as text end repeat set my theResult to theURLStrings end searchByGoogle: |
Safariのダウンロードフォルダを求めるv4
本ScriptはSafariのダウンロードフォルダのパスを求めるためのものですが、macOSおよびSafariのアップデートによって動かなくなりました。
現時点では設定値からダウンロードフォルダを知ることはできないようです。セキュリティ上「できては困る」という判断が行われたものでしょうか。
なので、Spotlightの機能を用いて、「Download」フォルダを検索し、見つかったものの一覧からユーザーに選んでもらうとか、デフォルト状態のDownloadフォルダを指定してみて存在しなかったらユーザーに選択してもらうといった方法になると思われます。
AppleScript名:Safariのダウンロードフォルダを求めるv4 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" getSafariDownloadFolder() of me –> alias "Cherry:Users:me:Downloads:" –By Phillip Aker, Mark J Reed on getSafariDownloadFolder() set preferencesPath to POSIX path of (path to preferences) set bundleId to bundle identifier of (info for (path to application "Safari")) set safariPrefsFile to preferencesPath & bundleId & ".plist" tell application "System Events" to set folderName to get value of property list item "DownloadsPath" of property list file safariPrefsFile set pathString to current application’s NSString’s stringWithString:folderName set dlPath to pathString’s stringByExpandingTildeInPath() as string return (POSIX file dlPath) as alias end getSafariDownloadFolder |
ダークモード/ライトモードの切り替え
Dark Modeのトグル切り替えを行うAppleScriptです。
System Eventsに問い合わせて、appearance preferencesのDark Modeがtrueならダークモード、Dark Modeがfalseならライトモード(通常モード)です。
# Mac App Store用アプリケーションの中で使うとリジェクトの原因になります
AppleScript名:ダークモードの検出 v2 |
tell application "System Events" tell appearance preferences set dark mode to not dark mode end tell end tell |
Safariの最前面のWindowに新規tabをURLを指定しつつ作成
AppleScript名:Safariの最前面のWindowに新規tabをURLを指定しつつ作成 |
tell application "Safari" tell window 1 make new tab with properties {URL:"http://piyocast.com/as/"} end tell end tell |