20260615

【フォルダカスタマイズ】今日の日付のフォルダの作成 フォルダ選択 合字版

【フォルダカスタマイズ】今日の日付のフォルダの作成 フォルダ選択 合字版

NOTE記事一覧ですnote.com
 

【Safari・FireFox用Script Editorで開く】 |

デスクトップに今日の日付のフォルダ合字版.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006デスクトップに今日の日付のフォルダを作ります
007
008月と日の合字文字が入ります
009色はカスタマイズしてください
010
011v1 初回作成
012v1.1 色指定部分の誤りを訂正
013v1.2 パスのエスケープを処理するようにした
014v1.2 コマンドラインツール未インストールでもエラーにならないようにした
015
016
017com.cocolog-nifty.quicktimer.icefloe *)
018----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
019use AppleScript version "2.8"
020use framework "Foundation"
021use framework "AppKit"
022use scripting additions
023
024property refMe : a reference to current application
025
026#辞書
027set recordWeekDayColor to {||:"レッド", ||:"グレイ", ||:"カラーなし", ||:"グリーン", ||:"カラーなし", ||:"オレンジ", ||:"ブルー"} as record
028set ocidTagDict to refMe's NSMutableDictionary's alloc()'s init()
029ocidTagDict's setDictionary:(recordWeekDayColor)
030#月レコード
031set recordMonth to {|01|:"", |02|:"", |03|:"", |04|:"", |05|:"", |06|:"", |07|:"", |08|:"", |09|:"", |10|:"", |11|:"", |12|:""} as record
032set ocidMonthDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordMonth)
033#日レコード
034set recordDate to {|01|:"", |02|:"", |03|:"", |04|:"", |05|:"", |06|:"", |07|:"", |08|:"", |09|:"", |10|:"", |11|:"", |12|:"", |13|:"", |14|:"", |15|:"", |16|:"", |17|:"", |18|:"", |19|:"", |20|:"", |21|:"", |22|:"", |23|:"", |24|:"", |25|:"", |26|:"", |27|:"", |28|:"", |29|:"", |30|:"", |31|:""} as record
035set ocidDateDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordDate)
036
037#デスクトップ
038set appFileManager to refMe's NSFileManager's defaultManager()
039set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
040set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
041#日付情報の取得
042set ocidDate to refMe's NSDate's |date|()
043set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
044ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
045ocidNSDateFormatter's setDateFormat:("yyyyMMdd")
046set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
047set strDateAndTime to ocidDateAndTime as text
048ocidNSDateFormatter's setDateFormat:("EEE")
049set ocidWeekDay to ocidNSDateFormatter's stringFromDate:(ocidDate)
050set strWeekDay to ocidWeekDay as text
051ocidNSDateFormatter's setDateFormat:("dd")
052set ocidDayNo to ocidNSDateFormatter's stringFromDate:(ocidDate)
053set strDayNo to ocidDayNo as text
054set ocidDateValue to ocidDateDict's objectForKey:(ocidDayNo)
055ocidNSDateFormatter's setDateFormat:("MM")
056set ocidMonthNo to ocidNSDateFormatter's stringFromDate:(ocidDate)
057set strMonthNo to ocidMonthNo as text
058set ocidMonthValue to ocidMonthDict's objectForKey:(ocidMonthNo)
059set ocidSetJsonValue to ocidMonthValue's stringByAppendingString:(ocidDateValue)
060
061#
062set ocidMakeDirPathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(ocidDateAndTime) isDirectory:(true)
063set ocidMakeDirPath to ocidMakeDirPathURL's |path|()
064
065#フォルダを先に作っておく
066set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
067ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
068set listDone to appFileManager's createDirectoryAtURL:(ocidMakeDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
069#テンポラリ
070set ocidSymbolName to refMe's NSString's stringWithString:("calendar")
071set ocidTempDirURL to appFileManager's temporaryDirectory()
072set ocidUUID to refMe's NSUUID's alloc()'s init()
073set ocidUUIDString to ocidUUID's UUIDString()
074set ocidSaveDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true))
075set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
076(ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions))
077set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference))
078#JSONパス
079set strSetSymbolName to ("" & ocidDayNo & "." & ocidSymbolName & "") as text
080set strJsonFileName to ("" & strSetSymbolName & ".json") as text
081set ocidSaveJsonPathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strJsonFileName) isDirectory:(false))
082set strSaveJsonPath to ocidSaveJsonPathURL's |path|() as text
083#JSONの中身
084set ocidJsonDict to refMe's NSMutableDictionary's alloc()'s init()
085(ocidJsonDict's setObject:(ocidSetJsonValue) forKey:("emoji"))
086set ocidOption to (refMe's NSJSONReadingJSON5Allowed)
087set listResponse to (refMe's NSJSONSerialization's dataWithJSONObject:(ocidJsonDict) options:(ocidOption) |error|:(reference))
088set ocidSaveData to (first item of listResponse)
089set ocidOption to (refMe's NSDataWritingAtomic)
090set listDone to (ocidSaveData's writeToURL:(ocidSaveJsonPathURL) options:(ocidOption) |error|:(reference))
091#xattr
092set strSaveJsonPath to doPathEscape(strSaveJsonPath)
093set ocidMakeDirPath to doPathEscape(ocidMakeDirPath)
094set strCmdText to ("/usr/bin/xattr -x -w com.apple.icon.folder#S \"$(/usr/bin/xxd -p \"" & strSaveJsonPath & "\" | tr -d \"\\\n\")\" \"" & ocidMakeDirPath & "\"") as text
095try
096   do shell script strCmdText
097on error strErrMsg number numErrNo
098   log strErrMsg & numErrNo
099   return false
100end try
101#SetFile
102try
103   #まずxcode-selectコマンドを実行してエラーになれなければ実行する
104   set strCmdText to ("/usr/bin/xcode-select -p") as text
105   do shell script strCmdText
106   #--> ここでエラーしても大丈夫なんだけど
107   set strCmd to ("/usr/bin/SetFile -a C \"" & ocidMakeDirPath & "\"") as text
108   do shell script strCmd
109on error strErrMes number numErrNo
110   log strCmdText & strErrMes
111   #エラーにはなるんだけど止めない
112end try
113######
114set ocidSetValue to ocidTagDict's objectForKey:(ocidWeekDay)
115set ocidTagName to (refMe's NSString's stringWithString:(ocidSetValue))
116set ocidTagArray to refMe's NSMutableArray's alloc()'s init()
117(ocidTagArray's addObject:(ocidTagName))
118set listResult to (ocidMakeDirPathURL's setResourceValue:(ocidTagArray) forKey:(refMe's NSURLTagNamesKey) |error|:(reference))
119
120return listResult
121
122
123
124
125########################
126#パスのエスケープ
127to doPathEscape(strFilePath)
128   set strFilePath to strFilePath as text
129   set listEscChar to {"\\", "\"", "$", "`"} as list
130   repeat with itemEscChar in listEscChar
131      set strSearchText to (itemEscChar) as text
132      set strReplaceText to ("\\" & itemEscChar & "") as text
133      set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
134   end repeat
135   
136   set strSearchText to ("!") as text
137   set strReplaceText to ("\"'!'\"") as text
138   set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
139   
140   return strFilePath
141end doPathEscape
142########################
143#置換
144to doReplace(argOrignalText, argSearchText, argReplaceText)
145   set strDelim to AppleScript's text item delimiters
146   set AppleScript's text item delimiters to argSearchText
147   set listDelim to every text item of argOrignalText
148   set AppleScript's text item delimiters to argReplaceText
149   set strReturn to listDelim as text
150   set AppleScript's text item delimiters to strDelim
151   return strReturn
152end doReplace
AppleScriptで生成しました