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 |
デスクトップ上の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 |
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 |
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の最前面のドキュメントのテキストを取得する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 |
指定のテキストファイルをオープンする
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で文字数をカウントする
AppleScript名:TextEditで文字数をカウントする |
tell application "TextEdit" tell front document set a to text of it end tell set tLen to length of a display dialog (tLen as string) & "文字です" with title "この文章の文字数は…" buttons {"OK"} default button 1 with icon 1 end tell –18058 |
TextEditの本文取得
AppleScript名:TextEditの本文取得 |
tell application "TextEdit" tell front document set a to text of it end tell end tell |
CotEditor 3の選択範囲を書き換える
AppleScript名:CotEditor 3の選択範囲を書き換える |
tell application "CotEditor" tell document 1 properties of selection –> {range:{0, 4}, class:text selection, line range:{1, 1}, contents:"似ている"} set contents of selection to "ほぼおなじ" end tell end tell |
CotEditorでオープン中の最前面の書類の本文テキストを取得する
AppleScript名:CotEditorでオープン中の最前面の書類の本文テキストを取得する |
set aRes to getCotEditorText() of me
on getCotEditorText() tell application "CotEditor" if (count every document) = 0 then return false tell front document set aText to contents end tell end tell end getCotEditorText |
表示中のCotEditor書類の「次」のファイルを縦書きでオープン
AppleScript名:表示中のCotEditor書類の「次」のファイルを縦書きでオープン |
— Created 2017-12-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –http://piyocast.com/as/archives/5034 property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property SMSForder : a reference to current application’s SMSForder property NSPredicate : a reference to current application’s NSPredicate property NSFileManager : a reference to current application’s NSFileManager property NSMutableArray : a reference to current application’s NSMutableArray property NSSortDescriptor : a reference to current application’s NSSortDescriptor property NSURLIsPackageKey : a reference to current application’s NSURLIsPackageKey property NSURLIsDirectoryKey : a reference to current application’s NSURLIsDirectoryKey property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles property NSDirectoryEnumerationSkipsPackageDescendants : a reference to current application’s NSDirectoryEnumerationSkipsPackageDescendants property NSDirectoryEnumerationSkipsSubdirectoryDescendants : a reference to current application’s NSDirectoryEnumerationSkipsSubdirectoryDescendants load framework tell application "CotEditor" set dCount to count every document if dCount = 0 then return tell front document set curPath to path end tell tell window 1 set aBounds to bounds end tell end tell set aPath to NSString’s stringWithString:curPath set fileName to (aPath’s lastPathComponent()) –ファイル名 set pathExtension to aPath’s pathExtension() as string set parentFol to (aPath’s stringByDeletingLastPathComponent()) as string —親フォルダ –同じフォルダから同じ拡張子のファイルのファイル名を取得 set fList to my getFilesByIncludedStringInName:(pathExtension) fromDirectory:(parentFol) exceptPackages:(true) –昇順ソート set aArray to NSArray’s arrayWithArray:fList set desc1 to NSSortDescriptor’s sortDescriptorWithKey:"self" ascending:true selector:"localizedCaseInsensitiveCompare:" set bArray to aArray’s sortedArrayUsingDescriptors:{desc1} –ファイル名検索 set aIndex to (SMSForder’s indexesOfItem:fileName inArray:bArray inverting:false) as list if aIndex = {} then display notification "Error: File Not Found" return end if set bIndex to (contents of first item of aIndex) + 1 + 1 –0 based to 1 based conversion & next one set aLen to length of (bArray as list) if bIndex > aLen then display notification "Error: Out of bounds" return end if set newFile to contents of item bIndex of (bArray as list) set newPath to parentFol & "/" & newFile tell application "CotEditor" set oldDoc to front document open (POSIX file newPath) as alias tell window 1 set bounds to aBounds end tell close oldDoc without saving end tell makeWinVertical() of me –縦書き表示 –指定フォルダ内の指定文字列を含むファイル名のlistを抽出する on getFilesByIncludedStringInName:(fileNameStr as string) fromDirectory:(sourceFolder) exceptPackages:(packageF as boolean) set fileManager to NSFileManager’s defaultManager() set aURL to |NSURL|’s fileURLWithPath:sourceFolder set theOptions to (NSDirectoryEnumerationSkipsPackageDescendants as integer) + (NSDirectoryEnumerationSkipsHiddenFiles as integer) + (NSDirectoryEnumerationSkipsSubdirectoryDescendants as integer) set directoryContents to fileManager’s contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:theOptions |error|:(missing value) set findPredicates to NSPredicate’s predicateWithFormat_("lastPathComponent CONTAINS %@", fileNameStr) set foundItemList to directoryContents’s filteredArrayUsingPredicate:findPredicates –Remove Folders From found URL Array set anArray to NSMutableArray’s alloc()’s init() repeat with i in foundItemList set j to contents of i set {theResult, isDirectory} to (j’s getResourceValue:(reference) forKey:(NSURLIsDirectoryKey) |error|:(missing value)) –Collect files if (isDirectory as boolean = false) then (anArray’s addObject:j) else if (packageF = false) then –Allow Package files? set {theResult, isPackage} to (j’s getResourceValue:(reference) forKey:(NSURLIsPackageKey) |error|:(missing value)) if (isPackage as boolean) = true then (anArray’s addObject:j) end if end if end repeat return (anArray’s valueForKey:"lastPathComponent") as list end getFilesByIncludedStringInName:fromDirectory:exceptPackages: –Make CotEditor’s front window to Vertical display mode (Tategaki) on makeWinVertical() activate application "CotEditor" tell application "System Events" tell process "CotEditor" try click menu item "縦書きで表示" of menu 1 of menu bar item "フォーマット" of menu bar 1 end try end tell end tell end makeWinVertical |