CotEditor内のScript Menuに入れて、現在オープン中の(連番つき)テキストファイルと同一フォルダに入っているテキストファイルのうち、番号が「前」に該当するファイルのXattr(拡張属性)を操作して、CotEditorの縦書き属性を追加し、CotEditorのファイルオープン時にデフォルトで縦書き表示を行うAppleScriptです。
前バージョンではGUI Scriptingを使っていたため、CotEditor内蔵Script MenuではなくOS側のScript Menuから呼び出すことしかできませんでした。本バージョンでは、CotEditor内蔵メニューから呼び出せます。
CotEditor内蔵Script Menuから呼べると、ファイル名に指定の特殊文字を入れておくことで、キーボードショートカットから呼び出せるようになります。
–> XAttribute.framework (To ~/Library/Frameworks/)
AppleScript名:表示中のCotEditor書類の「前」のファイルを縦書きでオープン v3 |
— Created 2017-12-15 by Takaaki Naganoya — Modified 2018-02-28 by Takaaki Naganoya — 2018 Piyomaru Software –v3:Xattrに追記してCotEditorでデフォルト縦書き表示を行わせた。CotEditorのアプリケーション内のScript Menuから実行可能に use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "XAttribute" –https://github.com/rylio/OTMXAttribute 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 –returns POSIX path, not alias or file 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 & 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 –Add Vertical Xattribute (for CotEditor only) set xRes to addXAttrToFile(newPath, "com.coteditor.VerticalText", "1") of me –Open Previous Document 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 –指定フォルダ内の指定文字列を含むファイル名のファイルを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: on addXAttrToFile(aFile, anXattr, aValue) –Get Xattr String set anAttribute to (current application’s OTMXAttribute’s stringAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if anAttribute is not equal to missing value then –Already Exists set tmpRes to removeXAttrFromFile(aFile, anXattr) of me if tmpRes = false then return false end if –Set Xattr set xRes to (current application’s OTMXAttribute’s setAttributeAtPath:aFile |name|:anXattr value:aValue |error|:(missing value)) if xRes = missing value then return false return (xRes as boolean) end addXAttrToFile 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 |
More from my site
(Visited 64 times, 1 visits today)