20260531

ファイルやフォルダのロック設定・解除 OBJC FileManager NSFileImmutableを使う方法

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

NOTE記事一覧ですnote.com

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

ロック設定・解除OBJC-FileManager.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006NSFileManager の Attributes を使う方法
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 appFileManager to refMe's NSFileManager's defaultManager()
068      set listResponse to (appFileManager's attributesOfItemAtPath:(ocidFilePath) |error|:(reference))
069      set ocidAttrDict to (first item of listResponse)
070      set ocidKey to (refMe's NSFileImmutable)
071      set boolIsLock to (ocidAttrDict's objectForKey:(ocidKey))
072      if boolIsLock = (missing value) then
073         set strMsg to (ocidFilePath & return & "エラー:値が取得できない") as text
074         display alert strMsg
075         return false
076      else
077         set boolIsLock to boolIsLock as boolean
078      end if
079      
080      #逆をセットする
081      set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
082      if boolIsLock is true then
083         (ocidAttrDict's setObject:(false) forKey:(refMe's NSFileImmutable))
084      else if boolIsLock is false then
085         (ocidAttrDict's setObject:(true) forKey:(refMe's NSFileImmutable))
086      end if
087      set listDone to (appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference))
088      
089      set boolDone to (first item of listDone) as boolean
090      if boolDone is false then
091         set strMsg to (ocidFilePath & return & "エラー:たぶん管理者権限が必要です") as text
092         display alert strMsg
093         return false
094      end if
095   end repeat
096   
097   
098   return boolDone
099end open
AppleScriptで生成しました