20260615

【フォルダカスタマイズ】今日の日付のフォルダの作成 フォルダ選択 SFシンボル版

【フォルダカスタマイズ】今日の日付のフォルダの作成 フォルダ選択 SFシンボル版

NOTE記事一覧ですnote.com
 

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

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