
ファイルやフォルダのロック設定・解除 AS 記法 SH chflagsを利用
【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | |
| 006 | クラッシックなAS記法で全部やる場合 |
| 007 | ファイル・フォルダのロックとロックの解除 |
| 008 | SHでのコマンドによる設定 |
| 009 | |
| 010 | |
| 011 | v1 初回作成 |
| 012 | v1.1 クラシックなAS記法でパスのエスケープをする方法に変更 |
| 013 | |
| 014 | com.cocolog-nifty.quicktimer.icefloe *) |
| 015 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 016 | use AppleScript version "2.8" |
| 017 | use scripting additions |
| 018 | |
| 019 | |
| 020 | ######################## |
| 021 | # RUN |
| 022 | # on run {listAliasFilePath} |
| 023 | |
| 024 | on run |
| 025 | |
| 026 | ######################## |
| 027 | #ダイアログ |
| 028 | set strMsg to ("ファイル 選択") as text |
| 029 | set strPrompt to ("ファイル を選択してください" & return & "複数選択可") as text |
| 030 | set aliasDesktopDirPath to (path to desktop from user domain) as alias |
| 031 | set listUTI to {"public.item"} as list |
| 032 | try |
| 033 | tell application "SystemUIServer" |
| 034 | activate |
| 035 | # set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list |
| 036 | |
| 037 | |
| 038 | set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles, multiple selections allowed and showing package contents) as list |
| 039 | |
| 040 | end tell |
| 041 | on error strErrMes number numErrNo |
| 042 | log strErrMes & numErrNo |
| 043 | return false |
| 044 | end try |
| 045 | if listAliasFilePath is {} then |
| 046 | return false |
| 047 | end if |
| 048 | |
| 049 | |
| 050 | set boolDone to (open listAliasFilePath) |
| 051 | return boolDone |
| 052 | end run |
| 053 | |
| 054 | ######################## |
| 055 | # OPEN |
| 056 | on open listAliasFilePath |
| 057 | |
| 058 | |
| 059 | repeat with itemAliasFilePath in listAliasFilePath |
| 060 | set aliasItemFilePath to itemAliasFilePath as alias |
| 061 | set strFilePath to (POSIX path of aliasItemFilePath) as text |
| 062 | |
| 063 | #現在の値の取得 |
| 064 | set refResponse to doGetAttr(strFilePath) |
| 065 | if refResponse = (missing value) then |
| 066 | set strMsg to (strFilePath & return & "エラー:コマンドで値が取得できない") as text |
| 067 | tell application "System Events" |
| 068 | activate |
| 069 | display alert strMsg |
| 070 | end tell |
| 071 | return false |
| 072 | else |
| 073 | set boolIsLock to refResponse as boolean |
| 074 | end if |
| 075 | #逆の値をセット |
| 076 | if boolIsLock is false then |
| 077 | set boolDone to doSetFlag(strFilePath, false) |
| 078 | else if boolIsLock is true then |
| 079 | set boolDone to doSetFlag(strFilePath, true) |
| 080 | end if |
| 081 | |
| 082 | end repeat |
| 083 | |
| 084 | |
| 085 | return boolDone |
| 086 | end open |
| 087 | |
| 088 | |
| 089 | ######################## |
| 090 | #逆の値をセット |
| 091 | to doSetFlag(strFilePath, boolIsLock) |
| 092 | #設定する値 |
| 093 | set boolIsLock to boolIsLock as boolean |
| 094 | #パスをエスケープして |
| 095 | set strFilePath to doPathEscape(strFilePath) as text |
| 096 | #逆になるようにコマンド整形 |
| 097 | if boolIsLock is true then |
| 098 | set strCmd to ("/usr/bin/chflags nouchg \"" & strFilePath & "\"") as text |
| 099 | else if boolIsLock is false then |
| 100 | set strCmd to ("/usr/bin/chflags uchg \"" & strFilePath & "\"") as text |
| 101 | end if |
| 102 | #コマンド実行 |
| 103 | try |
| 104 | set strStdOut to (do shell script strCmd) |
| 105 | on error strErrMsg number numErrNo |
| 106 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 107 | return false |
| 108 | end try |
| 109 | |
| 110 | return true |
| 111 | |
| 112 | end doSetFlag |
| 113 | |
| 114 | ######################## |
| 115 | #現在の値を取得 |
| 116 | to doGetAttr(strFilePath) |
| 117 | #パスをエスケープして |
| 118 | set strFilePath to doPathEscape(strFilePath) as text |
| 119 | #値を取得 |
| 120 | set strCmd to ("/usr/bin/stat -f \"%Sf\" \"" & strFilePath & "\"") as text |
| 121 | try |
| 122 | set strStdOut to (do shell script strCmd) |
| 123 | on error strErrMsg number numErrNo |
| 124 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 125 | return missing value |
| 126 | end try |
| 127 | #uchgが含まれている=ロックされている |
| 128 | if strStdOut contains "uchg" then |
| 129 | set boolIsLock to true as boolean |
| 130 | else |
| 131 | set boolIsLock to false as boolean |
| 132 | end if |
| 133 | return boolIsLock |
| 134 | end doGetAttr |
| 135 | |
| 136 | ######################## |
| 137 | #パスのエスケープ |
| 138 | to doPathEscape(strFilePath) |
| 139 | |
| 140 | set listEscChar to {"\\", "\"", "$", "`"} as list |
| 141 | repeat with itemEscChar in listEscChar |
| 142 | set strSearchText to (itemEscChar) as text |
| 143 | set strReplaceText to ("\\" & itemEscChar & "") as text |
| 144 | set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText) |
| 145 | end repeat |
| 146 | |
| 147 | set strSearchText to ("!") as text |
| 148 | set strReplaceText to ("\"'!'\"") as text |
| 149 | set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText) |
| 150 | |
| 151 | return strFilePath |
| 152 | end doPathEscape |
| 153 | |
| 154 | ######################## |
| 155 | #置換 |
| 156 | to doReplace(argOrignalText, argSearchText, argReplaceText) |
| 157 | set strDelim to AppleScript's text item delimiters |
| 158 | set AppleScript's text item delimiters to argSearchText |
| 159 | set listDelim to every text item of argOrignalText |
| 160 | set AppleScript's text item delimiters to argReplaceText |
| 161 | set strReturn to listDelim as text |
| 162 | set AppleScript's text item delimiters to strDelim |
| 163 | return strReturn |
| 164 | end doReplace |
| AppleScriptで生成しました | |
