AppleScript NSFileAttributes

[NSFileAttributeKey]アクセス権(属性)を設定する

NSFileImmutable以外は使うことないかな…

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
パーミッション8進数についてはこちらを参考にしてください
[NSFilePosixPermissions]Permission MODE 8進数 10進数 一覧
https://quicktimer.cocolog-nifty.com/icefloe/2022/10/post-ce59ea.html
パーミンション番号8進数→10進数変換
https://quicktimer.cocolog-nifty.com/icefloe/cat76052288/index.html
*)
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application

########################
### ファイルパス
set strFilePath to ("~/Desktop/テストファイル.txt") as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
###空のファイル作成
set ocidBlankText to refMe's NSString's stringWithString:("")
set listDone to ocidBlankText's writeToURL:(ocidFilePathURL) atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
###アクセス権777-->511
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
if (item 1 of listDone) is false then
  set ocidNSErrorData to (item 2 of listDone)
  log "エラーコード:" & ocidNSErrorData's code() as text
  log "エラードメイン:" & ocidNSErrorData's domain() as text
  log "Description:" & ocidNSErrorData's localizedDescription() as text
  log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
end if
####################
###フォルダに対してのアクセス権設定
set strDirPath to ("~/Desktop/テストフォルダ/") as text
set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true)
###アクセス権777-->511
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listDone to appFileManager's createDirectoryAtURL:(ocidDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
if (item 1 of listDone) is false then
  set ocidNSErrorData to (item 2 of listDone)
  log "エラーコード:" & ocidNSErrorData's code() as text
  log "エラードメイン:" & ocidNSErrorData's domain() as text
  log "Description:" & ocidNSErrorData's localizedDescription() as text
  log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
end if
####################
###NSFileAppendOnly 追加のみ だが追加したファイルやフォルダを削除できなくなる
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(refMe's NSNumber's numberWithBool:true) forKey:(refMe's NSFileAppendOnly)
#ファイルに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
#ディレクトリに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)
## return "状態確認する場合"
##解除
ocidAttrDict's setValue:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSFileAppendOnly)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)


####################
###NSFileExtensionHidden 拡張子を隠すか?だが Finderの拡張子の表示設定が優先される
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(refMe's NSNumber's numberWithBool:true) forKey:(refMe's NSFileExtensionHidden)
#ファイルに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
#ディレクトリに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)
## return "状態確認する場合"
##解除
ocidAttrDict's setValue:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSFileExtensionHidden)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)


####################
###NSFileImmutable chflagsの uchg相当 ユーザーレベル Findeのロック
###Finder フォルダ ファイルアイコンに鍵がつく
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(refMe's NSNumber's numberWithBool:true) forKey:(refMe's NSFileImmutable)
#ファイルに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
#ディレクトリに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)
## return "状態確認する場合"
##解除
ocidAttrDict's setValue:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSFileImmutable)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)


####################
###NSFileProtectionKey ファイルの保護のモードを指定できる
(* 設定できる値は 暗号化の方法
NSFileProtectionComplete
NSFileProtectionCompleteUnlessOpen
NSFileProtectionCompleteUntilFirstUserAuthentication
NSFileProtectionNone
NSFileProtectionCompleteWhenUserInactive
*)
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(refMe's NSFileProtectionCompleteUnlessOpen) forKey:(refMe's NSFileProtectionKey)
#ファイルに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
#ディレクトリに追加
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)
## return "状態確認する場合"
##解除
ocidAttrDict's setValue:(refMe's NSFileProtectionNone) forKey:(refMe's NSFileProtectionKey)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)



return


|

[NSFileManager]Permission ファイル フォルダのアクセス権の設定


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
パーミッション8進数についてはこちらを参考にしてください
[NSFilePosixPermissions]Permission MODE 8進数 10進数 一覧
https://quicktimer.cocolog-nifty.com/icefloe/2022/10/post-ce59ea.html
パーミンション番号8進数→10進数変換
https://quicktimer.cocolog-nifty.com/icefloe/cat76052288/index.html
*)
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application

########################
#### ファイルに対してのアクセス権設定
set strFilePath to ("~/Library/Mail/PersistenceInfo.plist") as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
###
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference)
if (item 1 of listDone) is false then
  set ocidNSErrorData to (item 2 of listDone)
  log "エラーコード:" & ocidNSErrorData's code() as text
  log "エラードメイン:" & ocidNSErrorData's domain() as text
  log "Description:" & ocidNSErrorData's localizedDescription() as text
  log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
end if
####################
###フォルダに対してのアクセス権設定
set strDirPath to ("~/Library/Mail/") as text
set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
###
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error|:(reference)
if (item 1 of listDone) is false then
  set ocidNSErrorData to (item 2 of listDone)
  log "エラーコード:" & ocidNSErrorData's code() as text
  log "エラードメイン:" & ocidNSErrorData's domain() as text
  log "Description:" & ocidNSErrorData's localizedDescription() as text
  log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
end if

|

フォルダを作って新規テキストファイル


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

###
set strText to ("美しい日本語のテキスト") as text
set ocidStrings to refMe's NSString's stringWithString:(strText)
##
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set ocidSaveDirPathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("フォルダの名前")
##
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("保存するファイル名.txt")
###
set listDone to ocidStrings's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
###ターミナルで実行できるように755アクセス権にする
set ocidFileAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
# 777-->511 755-->493 700-->448 766-->502
(ocidFileAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions))
set listDone to (appFileManager's setAttributes:(ocidFileAttrDict) ofItemAtPath:(ocidSaveFilePathURL's |path|()) |error|:(reference))

log item 1 of listDone
log item 2 of listDone

|

NSFileExtendedAttributes基本


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
#
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()

###パブリックフォルダ
set strFilePath to "~/Creative Cloud Files" as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()

###通常のAttributes
set listAttributesDict to appFileManager's attributesOfItemAtPath:(ocidFilePath) |error|:(reference)
set ocidAttributesDict to (item 1 of listAttributesDict)
set ocidAllKeyArray to ocidAttributesDict's allKeys()
###
repeat with itemKey in ocidAllKeyArray
  set ocidValue to (ocidAttributesDict's valueForKey:(itemKey))
  log itemKey as text
  if (className() of ocidValue as text) contains "Number" then
    log (ocidAttributesDict's valueForKey:(itemKey)) as integer
  else if (className() of ocidValue as text) contains "Date" then
    log (ocidAttributesDict's valueForKey:(itemKey)) as date
  else if (className() of ocidValue as text) contains "Boolean" then
    log (ocidAttributesDict's valueForKey:(itemKey)) as boolean
  else if (className() of ocidValue as text) contains "String" then
    log (ocidAttributesDict's valueForKey:(itemKey)) as text
  end if
  log "----+----1----+----2----+-----3----+----4----+----5"
end repeat
#####NSFileExtendedAttributesの有無
set ocidExtendAttr to ocidAttributesDict's valueForKey:("NSFileExtendedAttributes")
if ocidExtendAttr is (missing value) then
  log "NSFileExtendedAttributesが無い"
else
  log "NSFileExtendedAttributesがある"
  set ocidExtendAllKeysArray to ocidExtendAttr's allKeys()
  repeat with itemKey in ocidExtendAllKeysArray
    if (itemKey as text) is "com.apple.provenance" then
      log "たぶんアイコン付き"
      
    end if
  end repeat
end if
return





【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


set strFilePath to "~/Desktop/B.pdf"

set appFileManager to refMe's NSFileManager's defaultManager()

set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByExpandingTildeInPath()
####アトリビュートを取得
set listResponse to appFileManager's attributesOfItemAtPath:ocidFilePath |error|:(reference)
set ocidAttarDict to item 1 of listResponse
####レコード形式で戻り値がくるのでキーの名前を取得
log (ocidAttarDict's allKeys()) as list
log (count of ocidAttarDict)
####NSFileExtendedAttributesは子要素としてレコード形式なので
set ocidExtendAttr to ocidAttarDict's valueForKey:"NSFileExtendedAttributes"
-->ここがポイントNSFileExtendedAttributesがなければmissing valueが帰ります。
log ocidExtendAttr as list
####同様にレコードのキーを取得して対象をしぼると
log ocidExtendAttr's allKeys() as list





|

パーミッション8進→10進


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/FinderIcon.icns" as alias
set strDefaultAnswer to "666" as text

set strText to "777-->511\n775-->509\n770-->504\n755-->493\n750-->488\n700-->448\n644-->420"

try
  ###ダイアログを前面に出す
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
      activate
    end tell
  else
    tell current application
      activate
    end tell
  end if
  set recordResponse to (display dialog strText with title "3桁8進数を入力" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
  
on error
  log "エラーしました"
return "エラーしました"
  error number -128
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
  error number -128
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else
  log "エラーしました"
return "エラーしました"
  error number -128
end if

set strDem to doOct2Dem(strResponse)

try
  ###ダイアログを前面に出す
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
      activate
    end tell
  else
    tell current application
      activate
    end tell
  end if
  set recordResult to (display alert ("計算結果:" & strDem) buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" as informational giving up after 30)
on error
  log "エラーしました"
return
end try

###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
  set strText to strDem as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if

###################################
#####パーミッション 8進→10進
###################################

to doOct2Dem(argOctNo)
  set strOctalText to argOctNo as text
  set num3Line to first item of strOctalText as number
  set num2Line to 2nd item of strOctalText as number
  set num1Line to last item of strOctalText as number
  set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1)
return numDecimal
end doOct2Dem



|

ディスクが暗号化されているか?


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


####NSFileManagerで属性を収集する
set appFileManager to refMe's NSFileManager's defaultManager()
set listAttrDict to appFileManager's attributesOfItemAtPath:("/") |error|:(reference)
set ocidAttrDict to item 1 of listAttrDict
####NSFileProtectionKeyを確認
set ocidProtectionState to ocidAttrDict's valueForKey:(refMe's NSFileProtectionKey)

if (ocidProtectionState as text) contains "NSFileProtectionNone" then
return "暗号化されていません"
else
return "暗号化されています"
end if
(*
NSFileProtectionComplete
常時暗号化
NSFileProtectionCompleteUntilFirstUserAuthentication
※初回暗号化解除すればアクセス可能
NSFileProtectionCompleteUnlessOpen
ファイルを閉じるときに暗号化
NSFileProtectionNone
暗号化無し
*)
return



【スクリプトエディタで開く】|

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
########################

STR_STATUS=$(/usr/bin/fdesetup status)
/bin/echo "$STR_STATUS"
if [[ $STR_STATUS == *On* ]]; then
/bin/echo "暗号化されています"
else
/bin/echo "暗号化されていません"
fi

exit 0

|

NSFileExtendedAttributes削除

xattrにたよるしかない…

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

####ダイアログで使うデフォルトロケーション
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
###アプリケーション限定
set listUTI to {"public.item"}

####ダイアログを出す
set listFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, multiple selections allowed and showing package contents) as list



repeat with itemFilePath in listFilePath
  set strFilePath to POSIX path of itemFilePath as text
  
  set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
  set ocidFilePath to ocidFilePathStr's stringByExpandingTildeInPath()
  ###
  set listResponse to (appFileManager's attributesOfItemAtPath:ocidFilePath |error|:(reference))
  set ocidAttarDict to item 1 of listResponse
  set ocidExtendAttr to (ocidAttarDict's valueForKey:"NSFileExtendedAttributes")
  #################################
  ####個別方式
  if ocidExtendAttr is not (missing value) then
    set listExtendAttr to ocidExtendAttr's allKeys() as list
    ###
    repeat with itemExtendAttr in listExtendAttr
      set strExtendAttr to itemExtendAttr as text
      set strCommandText to ("/usr/bin/xattr -rd " & strExtendAttr & "  \"" & strFilePath & "\"") as text
      ##do shell script strCommandText
      set ocidCommandText to (refMe's NSString's stringWithString:strCommandText)
      set ocidTermTask to refMe's NSTask's alloc()'s init()
      (ocidTermTask's setLaunchPath:"/bin/zsh")
      (ocidTermTask's setArguments:({"-c", ocidCommandText}))
      set listDoneReturn to (ocidTermTask's launchAndReturnError:(reference))
      if (item 2 of listDoneReturn) is not (missing value) then
        log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text
        log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text
        log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text
        log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text
      end if
    end repeat
  end if
  ########################################
  ####まとめて削除方式
  ####コマンド整形
  set strCommandText to ("/usr/bin/xattr -rc  \"" & strFilePath & "\"") as text
  ##do shell script strCommandText
  set ocidCommandText to (refMe's NSString's stringWithString:strCommandText)
  set ocidTermTask to refMe's NSTask's alloc()'s init()
  (ocidTermTask's setLaunchPath:"/bin/zsh")
  (ocidTermTask's setArguments:({"-c", ocidCommandText}))
  set listDoneReturn to (ocidTermTask's launchAndReturnError:(reference))
  if (item 2 of listDoneReturn) is not (missing value) then
    log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text
    log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text
    log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text
    log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text
  end if
  
  
end repeat



|

ファイルに設定されているNSFileExtendedAttributesを削除する

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()



####ダイアログで使うデフォルトロケーション
tell application "Finder"
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell

###ANy
set listUTI to {"public.item"}
####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

set strFilePath to POSIX path of aliasFilePath
####ドキュメントのパスをNSString
set ocidFilePath to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePath's stringByStandardizingPath
set ocidNSUrlPath to refMe's NSURL's fileURLWithPath:ocidFilePath


###アトリビュートを取得
set ocidFileAttr to appFileManager's attributesOfItemAtPath:ocidFilePath |error|:(missing value)
###ExtendedAttributesを分離取得
set ocidExtendedAttrDict to (ocidFileAttr's valueForKey:"NSFileExtendedAttributes")
###設定されているキーを取得
set ocidAllKyesArray to ocidExtendedAttrDict's allKeys()
###キーの数だけ繰り返し
repeat with itemAllKyesArray in ocidAllKyesArray
  ###削除する
  (ocidExtendedAttrDict's removeObjectForKey:itemAllKyesArray)
end repeat
###確認
if (ocidExtendedAttrDict's valueForKey:"NSFileExtendedAttributes") is missing value then
  log "全て削除しました"
else
  log "何か残ってます"
end if


|

ファイルのロック ロック解除

【スクリプトエディタで開く】


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()



tell application "Finder"
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
set listUTI to {"public.item"}
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
##################
###元ファイル
##################
set strFilePath to POSIX path of aliasFilePath
set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false)

-->false
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
-->true
set ocidTrue to (refMe's NSNumber's numberWithBool:true)

####今のアトリビュートを取得して
set listAttrDict to appFileManager's attributesOfItemAtPath:ocidFilePath |error|:(reference)
set ocidAttrDict to item 1 of listAttrDict
set bookLock to ocidAttrDict's NSFileImmutable
####ロックされているなら
if bookLock = ocidTrue then
  ###ロック解除する
  appFileManager's setAttributes:{NSFileImmutable:ocidFalse} ofItemAtPath:ocidFilePath |error|:(reference)
else
  ###ロックする
  appFileManager's setAttributes:{NSFileImmutable:ocidTrue} ofItemAtPath:ocidFilePath |error|:(reference)
end if

|

[NSFileSize]ファイルサイズを取得する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#error number -128
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

set objFileManager to refMe's NSFileManager's defaultManager()


####################################################
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to music folder from user domain) as alias
end tell
###ダイアログテキスト
set strPromptText to "フォルダを選んでください" as text
####ダイアログを出す
try
set aliasDirPath to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
on error
log "エラーしました"
return
end try

####UNIXパスに
set strDirPath to POSIX path of aliasDirPath
####パスをNSString
set ocidDirPath to refNSString's stringWithString:strDirPath
####パスをNSURL
set ocidDirPathURL to refNSURL's alloc()'s initFileURLWithPath:ocidDirPath

#####ディレクトリのファイルリスト
set listFileListArray to objFileManager's contentsOfDirectoryAtURL:ocidDirPathURL includingPropertiesForKeys:{refMe's NSURLIsRegularFileKey} options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)

#####データ部
set ocidContentsArray to item 1 of listFileListArray
####エラー部
set ocidNSErrorData to item 2 of listFileListArray
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if


repeat with objContentsArrayItem in ocidContentsArray
####URLをパスにする
set ocidArrayItemFilePath to objContentsArrayItem's |path|()
###ファイルサイズ取得
set listAttribute to (objFileManager's attributesOfItemAtPath:ocidArrayItemFilePath |error|:(reference))
#####データ部
set ocidAttribute to item 1 of listAttribute
####エラー部
set ocidNSErrorData to item 2 of listFileListArray
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if
####ファイルサイズを取り出し
set ocidFileSize to ocidAttribute's NSFileSize
log ocidArrayItemFilePath as text
log ocidFileSize as text
end repeat




##############################################
## エラー発生時のログ用
##############################################


to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom