20260531

ファイルやフォルダのロック設定・解除 OBJC NSRUL NSURLIsUserImmutableKey

ファイルやフォルダのロック設定・解除 OBJC NSRUL NSURLIsUserImmutableKey

NOTE記事一覧ですnote.com

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

ロック設定・解除OBJC-NSURL.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006NSRUL の ResourceValueを使う方法
007ファイル・フォルダのロックとロックの解除
008
009com.cocolog-nifty.quicktimer.icefloe *)
010----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use framework "UniformTypeIdentifiers"
015use scripting additions
016
017property refMe : a reference to current application
018
019########################
020# RUN
021#   on run {listAliasFilePath}
022
023on run
024   
025   ########################
026   #ダイアログ
027   set strMsg to ("ファイル 選択") as text
028   set strPrompt to ("ファイル を選択してください" & return & "複数選択可") as text
029   set aliasDesktopDirPath to (path to desktop from user domain) as alias
030   set listUTI to {"public.item"} as list
031   try
032      tell application "SystemUIServer"
033         activate
034         #   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
035         
036         
037         set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles, multiple selections allowed and showing package contents) as list
038         
039      end tell
040   on error strErrMes number numErrNo
041      log strErrMes & numErrNo
042      return false
043   end try
044   if listAliasFilePath is {} then
045      return false
046   end if
047   
048   
049   set boolDone to (open listAliasFilePath)
050   return boolDone
051end run
052
053########################
054# OPEN
055on open listAliasFilePath
056   
057   
058   repeat with itemAliasFilePath in listAliasFilePath
059      set aliasItemFilePath to itemAliasFilePath as alias
060      set strFilePath to (POSIX path of aliasItemFilePath) as text
061      set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
062      set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
063      set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
064      set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
065      set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false))
066      #現在の値を取得して
067      set ocidKey to (refMe's NSURLIsUserImmutableKey)
068      set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(ocidKey) |error|:(reference))
069      set boolIsLock to (second item of listResponse)
070      if boolIsLock = (missing value) then
071         set strMsg to (ocidFilePath & return & "エラー:値が取得できない") as text
072         display alert strMsg
073         return false
074      else
075         set boolDone to (first item of listResponse) as boolean
076         if boolDone is false then
077            set strMsg to (ocidFilePath & return & "エラー:値が取得できない") as text
078            display alert strMsg
079            return false
080         else if boolDone is true then
081            set boolIsLock to boolIsLock as boolean
082         end if
083      end if
084      #逆をセットする
085      if boolIsLock is true then
086         set ocidSetBool to (refMe's NSNumber's numberWithBool:(false))
087      else if boolIsLock is false then
088         set ocidSetBool to (refMe's NSNumber's numberWithBool:(true))
089      end if
090      set ocidKey to (refMe's NSURLIsUserImmutableKey)
091      set listDone to (ocidFilePathURL's setResourceValue:(ocidSetBool) forKey:(ocidKey) |error|:(reference))
092      set boolDone to (first item of listDone) as boolean
093      if boolDone is false then
094         set strMsg to (ocidFilePath & return & "エラー:たぶん管理者権限が必要です") as text
095         display alert strMsg
096         return false
097      end if
098   end repeat
099   
100   
101   return boolDone
102end open
AppleScriptで生成しました