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 色指定部分の誤りを訂正
013
014
015com.cocolog-nifty.quicktimer.icefloe *)
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use scripting additions
021
022property refMe : a reference to current application
023
024
025
026########################
027# RUN
028(*
029on run {listAliasFilePath}
030      *)
031on run
032   
033   ########################
034   #ダイアログ
035   set strMsg to ("フォルダ 選択") as text
036   set strPrompt to ("フォルダ を選択してください" & return & "複数選択可") as text
037   set aliasDesktopDirPath to (path to desktop from user domain) as alias
038   set listUTI to {"public.item"} as list
039   try
040      tell application "Finder"
041         #Finderから呼び出すことでスクリプトアプリケーションになってもダイアログがマルチリンガルになります
042         #By calling from Finder, the dialog becomes multilingual even if it becomes a script application.
043         tell application "SystemUIServer"
044            activate
045            set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles and multiple selections allowed without showing package contents) as list
046         end tell
047      end tell
048   on error strErrMes number numErrNo
049      log strErrMes & numErrNo
050      return false
051   end try
052   if listAliasFilePath is {} then
053      return false
054   end if
055   
056   
057   set boolDone to (open listAliasFilePath)
058   return boolDone
059end run
060
061########################
062# OPEN
063on open listAliasFilePath
064   
065   #リスト形式を確定させて
066   tell application "Finder"
067      set listAliasFilePath to listAliasFilePath as alias list
068   end tell
069   #順番に処理
070   repeat with itemAliasFilePath in listAliasFilePath
071      set aliasFilePath to itemAliasFilePath as alias
072      set recordInfoFor to (info for aliasFilePath) as record
073      set boolIsDir to (folder of recordInfoFor) as boolean
074      #フォルダの場合だけ次工程に回す
075      if boolIsDir is true then
076         set boolDone to doMakeCustomizeFolder(aliasFilePath)
077      end if
078   end repeat
079   return true
080   
081   
082end open
083
084
085
086
087########################
088# doMakeCustomizeFolder
089on doMakeCustomizeFolder(argAliasDirPath)
090   
091   #NSFileManager
092   set appFileManager to refMe's NSFileManager's defaultManager()
093   
094   #辞書
095   set recordWeekDayColor to {||:"レッド", ||:"グレイ", ||:"カラーなし", ||:"グリーン", ||:"カラーなし", ||:"オレンジ", ||:"ブルー"} as record
096   set ocidTagDict to refMe's NSMutableDictionary's alloc()'s init()
097   ocidTagDict's setDictionary:(recordWeekDayColor)
098   #月レコード
099   set recordMonth to {|01|:"", |02|:"", |03|:"", |04|:"", |05|:"", |06|:"", |07|:"", |08|:"", |09|:"", |10|:"", |11|:"", |12|:""} as record
100   set ocidMonthDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordMonth)
101   #日レコード
102   set 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
103   set ocidDateDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordDate)
104   
105   #パス
106   set aliasDirPath to argAliasDirPath as alias
107   set strDirPath to (POSIX path of aliasDirPath) as text
108   set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
109   set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
110   set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true)
111   
112   
113   
114   
115   #日付情報の取得
116   set ocidDate to refMe's NSDate's |date|()
117   set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
118   ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
119   ocidNSDateFormatter's setDateFormat:("yyyyMMdd")
120   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
121   set strDateAndTime to ocidDateAndTime as text
122   ocidNSDateFormatter's setDateFormat:("EEE")
123   set ocidWeekDay to ocidNSDateFormatter's stringFromDate:(ocidDate)
124   set strWeekDay to ocidWeekDay as text
125   ocidNSDateFormatter's setDateFormat:("dd")
126   set ocidDayNo to ocidNSDateFormatter's stringFromDate:(ocidDate)
127   set strDayNo to ocidDayNo as text
128   set ocidDateValue to ocidDateDict's objectForKey:(ocidDayNo)
129   ocidNSDateFormatter's setDateFormat:("MM")
130   set ocidMonthNo to ocidNSDateFormatter's stringFromDate:(ocidDate)
131   set strMonthNo to ocidMonthNo as text
132   set ocidMonthValue to ocidMonthDict's objectForKey:(ocidMonthNo)
133   set ocidSetJsonValue to ocidMonthValue's stringByAppendingString:(ocidDateValue)
134   
135   
136   
137   #テンポラリ
138   set ocidSymbolName to refMe's NSString's stringWithString:("calendar")
139   set ocidTempDirURL to appFileManager's temporaryDirectory()
140   set ocidUUID to refMe's NSUUID's alloc()'s init()
141   set ocidUUIDString to ocidUUID's UUIDString()
142   set ocidSaveDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true))
143   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
144   (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions))
145   set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference))
146   
147   #JSONパス
148   set strSetSymbolName to ("" & ocidDayNo & "." & ocidSymbolName & "") as text
149   set strJsonFileName to ("" & strSetSymbolName & ".json") as text
150   set ocidSaveJsonPathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strJsonFileName) isDirectory:(false))
151   set strSaveJsonPath to ocidSaveJsonPathURL's |path|() as text
152   
153   #JSONの中身
154   set ocidJsonDict to refMe's NSMutableDictionary's alloc()'s init()
155   (ocidJsonDict's setObject:(ocidSetJsonValue) forKey:("emoji"))
156   set ocidOption to (refMe's NSJSONReadingJSON5Allowed)
157   set listResponse to (refMe's NSJSONSerialization's dataWithJSONObject:(ocidJsonDict) options:(ocidOption) |error|:(reference))
158   set ocidSaveData to (first item of listResponse)
159   set ocidOption to (refMe's NSDataWritingAtomic)
160   set listDone to (ocidSaveData's writeToURL:(ocidSaveJsonPathURL) options:(ocidOption) |error|:(reference))
161   #xattr
162   set ocidDirPath to doPathEscape(ocidDirPath)
163   set strSaveJsonPath to doPathEscape(strSaveJsonPath)
164   set strCmdText to ("/usr/bin/xattr -x -w com.apple.icon.folder#S \"$(/usr/bin/xxd -p \"" & strSaveJsonPath & "\" | tr -d \"\\\n\")\" \"" & ocidDirPath & "\"") as text
165   try
166      do shell script strCmdText
167   on error strErrMsg number numErrNo
168      log strErrMsg & numErrNo
169      return false
170   end try
171   
172   #SetFile
173   try
174      #まずxcode-selectコマンドを実行してエラーになれなければ実行する
175      set strCmdText to ("/usr/bin/xcode-select -p") as text
176      do shell script strCmdText
177      #--> ここでエラーしても大丈夫なんだけど
178      set strCmd to ("/usr/bin/SetFile -a C \"" & ocidDirPath & "\"") as text
179      do shell script strCmd
180   on error strErrMes number numErrNo
181      log strCmdText & strErrMes
182      #エラーにはなるんだけど止めない
183   end try
184   ######
185   set ocidSetValue to ocidTagDict's objectForKey:(ocidWeekDay)
186   set ocidTagName to (refMe's NSString's stringWithString:(ocidSetValue))
187   set ocidTagArray to refMe's NSMutableArray's alloc()'s init()
188   (ocidTagArray's addObject:(ocidTagName))
189   set listResult to (ocidDirPathURL's setResourceValue:(ocidTagArray) forKey:(refMe's NSURLTagNamesKey) |error|:(reference))
190   
191   return (first item of listResult)
192   
193   
194end doMakeCustomizeFolder
195
196
197
198
199########################
200#パスのエスケープ
201to doPathEscape(strFilePath)
202   set strFilePath to strFilePath as text
203   set listEscChar to {"\\", "\"", "$", "`"} as list
204   repeat with itemEscChar in listEscChar
205      set strSearchText to (itemEscChar) as text
206      set strReplaceText to ("\\" & itemEscChar & "") as text
207      set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
208   end repeat
209   
210   set strSearchText to ("!") as text
211   set strReplaceText to ("\"'!'\"") as text
212   set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
213   
214   return strFilePath
215end doPathEscape
216########################
217#置換
218to doReplace(argOrignalText, argSearchText, argReplaceText)
219   set strDelim to AppleScript's text item delimiters
220   set AppleScript's text item delimiters to argSearchText
221   set listDelim to every text item of argOrignalText
222   set AppleScript's text item delimiters to argReplaceText
223   set strReturn to listDelim as text
224   set AppleScript's text item delimiters to strDelim
225   return strReturn
226end doReplace
AppleScriptで生成しました