— 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"
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 bArray’s indexOfObjectIdenticalTo:fileName
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 & previous 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 –縦書き表示
–指定フォルダ内の指定文字列を含むファイル名のファイルをPOSIX pathの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 3 of menu 1 of menu item "文章の方向" of menu 1 of menu bar item "フォーマット" of menu bar 1
end try
end tell
end tell
end makeWinVertical