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 |
タグ: 10.12savvy
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 |
最前面のウィンドウのタイトルを取得する2
AppleScript名:最前面のウィンドウのタイトルを取得する2 |
set aTitle to getPageTitleOfFrontmostWindow()
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 |
Safariで選択部分のテキストを取得する2
AppleScript名:Safariで選択部分のテキストを取得する2 |
tell application "Safari" set aRes to do JavaScript "var selObj = window.getSelection(); var selStr = (selObj).getRangeAt(0); unescape(selStr);" in document 1 end tell aRes |
Safariの最前面のドキュメントのテキストを取得するv2
AppleScript名:Safariの最前面のドキュメントのテキストを取得するv2 |
–フレームを使っていないことが前提条件 tell application "Safari" set wCount to count every window if wCount = 0 then display dialog "Safariでウィンドウがオープンされていません" buttons {"OK"} default button 1 return end if tell front document set aURL to URL –> http://piyocast.com/as/archives/155 set aTitle to name –> "SafariでURLをローディング – AppleScirpt Hole" –set aCon to text –log aCon end tell set aText to text of front document –本文テキスト end tell |
SafariでURLをローディング
本処理は最近のSafariでは正しくローディング検出できないようになってきたため、書き直したルーチン「SafariでURLローディング完了検出」を利用してください。
AppleScript名:SafariでURLをローディング |
set aURL to "http://www.piyocast.com/as/" –set aURL to "" tell application "Safari" set d to count every window if d = 0 then make new window tell document 1 set URL to aURL end tell else if aURL is not equal to "" then tell document 1 set URL to aURL end tell end if end if page_loaded(10) of me set a to do JavaScript "document.title" in document 1 end tell on page_loaded(timeout_value) repeat with i from 1 to (timeout_value * 10) tell application "Safari" if (do JavaScript "document.readyState" in document 1) is "complete" then return true else if i is the timeout_value then return false else delay 0.1 end if end tell end repeat return false end page_loaded |
TextEditで色置換 v2
AppleScript名:TextEditで色置換 v2 |
— Created 2018-01-09 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html load framework (* set targColor to {65535, 10023, 4626} set newColor to {19018, 0, 59109} repTextEditColor(targColor, newColor) of me *) set targColor to {19018, 0, 59109} set newColor to {65535, 10023, 4626} repTextEditColor(targColor, newColor) of me on repTextEditColor(targColor, newColor) set hitIndex to {} tell application "TextEdit" tell text of front document set colList to color of every attribute run end tell end tell set hitIndex to (current application’s SMSForder’s indexesOfItem:targColor inArray:(colList) inverting:false) as list tell application "TextEdit" tell text of front document repeat with i in hitIndex ignoring application responses set color of attribute run (i + 1) to newColor –0 based index to 1 based index conversion end ignoring end repeat end tell end tell end repTextEditColor |
指定のテキストファイルをオープンする
AppleScript名:指定のテキストファイルをオープンする |
set a to choose file file of type {"public.plain-text"} tell application "TextEdit" open a end tell |
TextEditでmake時にデータを与えつつ新規ドキュメント作成
AppleScript名:TextEditでmake時にデータを与えつつ新規ドキュメント作成 |
set a to "あいうえお"
tell application "TextEdit" make new document with properties {text:a} end tell |
TextEditの文章のうち赤くマークされた行をカウントする
macOS標準装備のテキストエディットで編集中の文章のうち、赤くマークされた行をカウントするAppleScriptです。
RGB値で抽出してカウントします。30年前からやっている原始レベルの処理です。値が同じ箇所を抽出するだけならバカでもできます。
いま現在AppleScriptで実現しているのは、「赤っぽい色」の箇所を抽出する「カラードメイン処理」です。RGB値だと値が1つでも違っていたら別物として判定されてしまいますが、カラードメイン処理では「赤系統の色」を計算して判定できるものです。
→ TextEditの文章のうち赤っぽい色でマークされた箇所をピックアップする v2
→ TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2
→ TextEdit本文色に応じて青っぽい色は男性の音声で、赤っぽい色は女性の音声で読み上げ
AppleScript名:TextEditの文章のうち赤くマークされた行をカウントする |
set rNum to 0
tell application "TextEdit" tell text of document 1 set pCount to (count paragraphs) repeat with i from 1 to pCount set aCol to color of paragraph i if aCol = {65535, 0, 0} then –{r, g, b} set rNum to rNum + 1 end if end repeat end tell end tell return {pCount, rNum} |