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