NSKeyedArchiver

[FontBook]フォントコレクションの作成(スタイル別で色々いっぺんに作成)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# フォントを追加したら再実行すればOKなやつ
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013##フォントブックを終了させる
014set strBundleID to ("com.apple.FontBook") as text
015set ocidResultsArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID))
016set numCntArray to ocidResultsArray's |count|() as integer
017repeat with itemNo from 0 to (numCntArray - 1) by 1
018  set ocidRunApp to (ocidResultsArray's objectAtIndex:(itemNo))
019  #通常終了を試みます
020  set boolDone to ocidRunApp's terminate()
021  if (boolDone) is true then
022    log strBundleID & ":正常終了"
023    #失敗したら
024  else if (boolDone) is false then
025    #強制終了を試みます
026    set boolDone to ocidRunApp's forceTerminate()
027    if (boolDone) is true then
028      log strBundleID & ":強制終了"
029    else if (boolDone) is false then
030      log strBundleID & ":終了出来ませんでした"
031    end if
032  end if
033end repeat
034
035##フルセットだと無い場合が多い
036set recordTraits to {|Bold|:(refMe's NSBoldFontMask), |Compressed|:(refMe's NSCompressedFontMask), |Expanded|:(refMe's NSExpandedFontMask), |Condensed|:(refMe's NSCondensedFontMask), |FixedPitch|:(refMe's NSFixedPitchFontMask), |Italic|:(refMe's NSItalicFontMask), |Narrow|:(refMe's NSNarrowFontMask), |NonStandard|:(refMe's NSNonStandardCharacterSetFontMask), |Poster|:(refMe's NSPosterFontMask), |SmallCaps|:(refMe's NSSmallCapsFontMask), |Unbold|:(refMe's NSUnboldFontMask), |Unitalic|:(refMe's NSUnitalicFontMask)} as record
037##一般的なセット
038# set recordTraits to {|Bold|:(refMe's NSBoldFontMask), |Compressed|:(refMe's NSCompressedFontMask), |Expanded|:(refMe's NSExpandedFontMask), |Condensed|:(refMe's NSCondensedFontMask), |FixedPitch|:(refMe's NSFixedPitchFontMask), |Italic|:(refMe's NSItalicFontMask)} as record
039#DICTにして
040set ocidTraitsDict to refMe's NSDictionary's alloc()'s initWithDictionary:(recordTraits)
041#キー名=ファイル名
042set ocidTraitsAllKeys to ocidTraitsDict's allKeys()
043#キーの数だけ繰り返し
044repeat with itemTraitsAllKeys in ocidTraitsAllKeys
045  #キー名=ファイル名
046  set strTraitsName to (itemTraitsAllKeys) as text
047  #TraitMask
048  set ocidTraitObjexct to (ocidTraitsDict's objectForKey:(itemTraitsAllKeys))
049  set ocidTraits to ocidTraitObjexct
050  ##保存先
051  set appFileManager to refMe's NSFileManager's defaultManager()
052  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
053  set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
054  set ocidSaveDirPathURL to (ocidLibraryDirPathURL's URLByAppendingPathComponent:("FontCollections") isDirectory:(true))
055  ##保存ファイル= collection
056  set ocidBaseFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strTraitsName) isDirectory:(false))
057  set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("collection"))
058  #ファイル名
059  set ocidFileName to ocidSaveFilePathURL's lastPathComponent()
060  ################################
061  #保存するDICTの初期化
062  set ocidPlistDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0))
063  ##【1】NSFontCollectionAttributes
064  set ocidSetCollectionDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0))
065  (ocidSetCollectionDict's setValue:(strTraitsName) forKey:("NSFontCollectionName"))
066  (ocidSetCollectionDict's setValue:(ocidFileName) forKey:("NSFontCollectionFileName"))
067  (ocidPlistDict's setObject:(ocidSetCollectionDict) forKey:("NSFontCollectionAttributes"))
068  
069  ##【2】NSFontCollectionFontDescriptors
070  set ocidSetDescriptorsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
071  ##NSFontManager
072  set appFontManager to refMe's NSFontManager's sharedFontManager()
073  #Maskしたリストを取り出す
074  set ocidFontNameArray to (appFontManager's availableFontNamesWithTraits:(ocidTraits))
075  set ocidFontFamilyArray to appFontManager's availableFontFamilies()
076  ##フォントの数だけ
077  repeat with itemFont in ocidFontNameArray
078    set ocidItemFont to (refMe's NSFont's fontWithName:(itemFont) |size|:(0.0))
079    set ocidDisplayName to ocidItemFont's |displayName|() as text
080    set ocidFontName to ocidItemFont's |fontName|() as text
081    set ocidItemFontDiscriptor to ocidItemFont's fontDescriptor()
082    (ocidSetDescriptorsArray's addObject:(ocidItemFontDiscriptor))
083    set ocidPostScripName to ocidItemFontDiscriptor's |postscriptName|() as text
084    set ocidFontName to (ocidItemFontDiscriptor's objectForKey:(refMe's NSFontNameAttribute)) as text
085  end repeat
086  (ocidPlistDict's setObject:(ocidSetDescriptorsArray) forKey:("NSFontCollectionFontDescriptors"))
087  ################################
088  #NSKeyedArchiver初期化
089  set ocidArchiverData to (refMe's NSKeyedArchiver's alloc()'s initRequiringSecureCoding:(false))
090  #エンコード
091  (ocidArchiverData's encodeObject:(ocidPlistDict) forKey:("NSFontCollectionDictionary"))
092  ocidArchiverData's finishEncoding()
093  set ocidEncData to ocidArchiverData's encodedData()
094  #保存するDATA
095  set ocidSaveData to (refMe's NSMutableData's alloc()'s initWithCapacity:(0))
096  (ocidSaveData's appendData:(ocidEncData))
097  
098  
099  ################################
100  #保存
101  set ocidOption to (refMe's NSDataWritingAtomic)
102  set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference))
103  if (item 2 of listDone) = (missing value) then
104    log "writeToURL 正常処理"
105  else if (item 2 of listDone) ≠ (missing value) then
106    log (item 2 of listDone)'s code() as text
107    log (item 2 of listDone)'s localizedDescription() as text
108    return "writeToURL エラーしました"
109  end if
110  
111end repeat
112
113delay 2
114
115tell application id strBundleID to activate
116
117
118return "Done"
119
120
AppleScriptで生成しました

|

[FontBook]フォントコレクションの作成(サンプルBOLD)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013set strTraitsName to ("Bold") as text
014set ocidTraits to (refMe's NSBoldFontMask)
015
016##保存先
017set appFileManager to refMe's NSFileManager's defaultManager()
018set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
019set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
020set ocidSaveDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("FontCollections") isDirectory:(true)
021##保存ファイル= collection
022set ocidBaseFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strTraitsName) isDirectory:(false)
023set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("collection")
024#ファイル名
025set ocidFileName to ocidSaveFilePathURL's lastPathComponent()
026################################
027#保存するDICTの初期化
028set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)
029##【1】NSFontCollectionAttributes
030set ocidSetCollectionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)
031ocidSetCollectionDict's setValue:(strTraitsName) forKey:("NSFontCollectionName")
032ocidSetCollectionDict's setValue:(ocidFileName) forKey:("NSFontCollectionFileName")
033ocidPlistDict's setObject:(ocidSetCollectionDict) forKey:("NSFontCollectionAttributes")
034
035##【2】NSFontCollectionFontDescriptors
036set ocidSetDescriptorsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
037##NSFontManager
038set appFontManager to refMe's NSFontManager's sharedFontManager()
039#Maskしたリストを取り出す
040set ocidFontNameArray to appFontManager's availableFontNamesWithTraits:(ocidTraits)
041set ocidFontFamilyArray to appFontManager's availableFontFamilies()
042##フォントの数だけ
043repeat with itemFont in ocidFontNameArray
044  set ocidItemFont to (refMe's NSFont's fontWithName:(itemFont) |size|:(0.0))
045  set ocidDisplayName to ocidItemFont's |displayName|() as text
046  set ocidFontName to ocidItemFont's |fontName|() as text
047  set ocidItemFontDiscriptor to ocidItemFont's fontDescriptor()
048  (ocidSetDescriptorsArray's addObject:(ocidItemFontDiscriptor))
049  set ocidPostScripName to ocidItemFontDiscriptor's |postscriptName|() as text
050  set ocidFontName to (ocidItemFontDiscriptor's objectForKey:(refMe's NSFontNameAttribute)) as text
051end repeat
052ocidPlistDict's setObject:(ocidSetDescriptorsArray) forKey:("NSFontCollectionFontDescriptors")
053################################
054#NSKeyedArchiver初期化
055set ocidArchiverData to refMe's NSKeyedArchiver's alloc()'s initRequiringSecureCoding:(false)
056#エンコード
057ocidArchiverData's encodeObject:(ocidPlistDict) forKey:("NSFontCollectionDictionary")
058ocidArchiverData's finishEncoding()
059set ocidEncData to ocidArchiverData's encodedData()
060#保存するDATA
061set ocidSaveData to refMe's NSMutableData's alloc()'s initWithCapacity:(0)
062ocidSaveData's appendData:(ocidEncData)
063
064
065################################
066#保存
067set ocidOption to (refMe's NSDataWritingAtomic)
068set listDone to ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)
069if (item 2 of listDone) = (missing value) then
070  log "writeToURL 正常処理"
071else if (item 2 of listDone) ≠ (missing value) then
072  log (item 2 of listDone)'s code() as text
073  log (item 2 of listDone)'s localizedDescription() as text
074  return "writeToURL エラーしました"
075end if
076
077
078return
079
080
AppleScriptで生成しました

|

com.apple.sharedfilelist

com.apple.sharedfilelist
macOS14で拡張子がsfl2からsfl3に変わった
キーアーカイブされたPLISTデータの取り扱いについて

リセットは今まで通り『sfltool』が利用できます
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-3140f3.html
ここでは、キー・アーカイブのことを
NSKeyedArchiverを圧縮
NSKeyedUnarchiverを解凍と呼んでいます

macOS13まので
圧縮 archivedDataWithRootObject
解凍 unarchiveObjectWithData 

非推奨から利用できなくなりましたので
macOS14では
圧縮 archivedDataWithRootObject:() requiringSecureCoding:() |error|:()
解凍 unarchivedObjectOfClass:(class)fromData:() |error|:() 

使う事になります
com.apple.sharedfilelistのsfl3ファイルはrequiringSecureCodingはfalse=NOが使われます。
macOS13までのスクリプトもこの部分を修正すればそのまま利用できます
Comapplesharedfilelist001

macOS14
圧縮:NSKeyedArchiver
202403201133261332x646

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

set listSaveData to (refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidReplaceDict) requiringSecureCoding:(true) |error|:(reference))
set ocidSaveData to (item 1 of listSaveData)
set ocidOption to (refMe's NSDataWritingFileProtectionNone)
set listDone to (ocidSaveData's writeToURL:(ocidSfl3FilePathURL) options:(ocidOption) |error|:(reference))
set boolDone to (item 1 of listDone) as boolean
if boolDone is false then
log (item 2 of listDone)'s localizedDescription() as text
return "保存に失敗しました"
end if

解凍:NSKeyedUnarchiver
202403201131291322x638

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

set ocidPlistData to (refMe's NSData's dataWithContentsOfURL:(ファイルのURL))
set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:((refMe's NSObject)'s class) fromData:(ocidPlistData) |error|:(reference)
set ocidPlistDict to (item 1 of listResponse)

LSSharedFileList カテゴリ
https://quicktimer.cocolog-nifty.com/icefloe/cat76055054/index.html

【1】圧縮:NSKeyedArchiver
[NSKeyedArchiver]plistをsfl3形式にKeyedArchiveする
https://quicktimer.cocolog-nifty.com/icefloe/2023/12/post-8e56a6.html


【2】解凍:NSKeyedUnarchiver
[NSKeyedUnarchiver]sfl3をplist形式にKeyedUnarchiveする
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-e2ea07.html

[LSSharedFileList] unarchivedObjectOfClassesで解凍する
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-2d749d.html

com.apple.LSSharedFileList.RecentServers.sfl3
[LSSharedFileList] unarchivedObjectOfClassesで解凍する
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-2d749d.html
[LSSharedFileList]最近使った項目のサーバー部分をリセットする(macOS14 sfl3対応)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-00119a.html

com.apple.LSSharedFileList.RecentHosts.sfl3
[LSSharedFileList]サーバーに接続の履歴部分をリセットする(macOS14 sfl3対応)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-5833e6.html

com.apple.LSSharedFileList.RecentDocuments.sfl3
[LSSharedFileList]最近使った項目の書類部分をリセットする(macOS14 sfl3対応)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-d344c8.html

com.apple.LSSharedFileList.RecentApplications.sfl3
[LSSharedFileList]最近使った項目のアプリケーション部分をリセットする(macOS14 sfl3対応)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-acaee1.html

com.apple.LSSharedFileList.ProjectsItems.sfl3
プロジェクトアイテムはラベルとタグのカテゴリをみてください
https://quicktimer.cocolog-nifty.com/icefloe/cat76054827/index.html

com.apple.LSSharedFileList.NetworkBrowser.sfl3
[sfl3 LSSharedFileList] com.apple.LSSharedFileList.NetworkBrowser.sfl3の設定変更 (macOS14対応版)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-601a41.html

com.apple.LSSharedFileList.iCloudItems.sfl3
[sfl3 LSSharedFileList]サイドバーiCloud編集(iCloudItems.sfl3)検索条件を加える macOS14対応版
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-3a956e.html

com.apple.LSSharedFileList.FavoriteVolumes.sfl3
[sfl3 FavoriteVolumes.sfl3]接続中のiPhoneを開く(sfl3 macOS14対応)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-30f29a.html
[sfl3 LSSharedFileList] com.apple.LSSharedFileList.FavoriteVolumes.sfl3の設定変更 (macOS14対応版)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-d202b0.html

com.apple.LSSharedFileList.FavoriteServers.sfl3
[LSSharedFileList]サーバーへ接続のよく使うサーバーを追加
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-41922b.html

com.apple.LSSharedFileList.FavoriteItems.sfl3
[sfl3 LSSharedFileList]Finderサイドバーのよく使う項目を追加する
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-7f4cb0.html
[FavoriteItems.sfl3]Finderサイドバーにフォルダを追加(macOS14対応版)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-e38f0e.html
[LSSharedFileList]サイドバーよく使う項目にiCloudを追加する(FavoriteItems.sfl3 macOS14対応版)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-3b822c.html

com.apple.LSSharedFileList.ApplicationRecentDocuments.sfl3
[ApplicationRecentDocuments]アプリケーションの最近使った項目をリセットする
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-0516ef.html

com.apple.LSSharedFileList.ApplicationRecentDocuments
[com.apple.sharedfilelist]最近使った書類を初期化する(com.apple.LSSharedFileList.ApplicationRecentDocuments)
https://quicktimer.cocolog-nifty.com/icefloe/2024/03/post-1556a6.html

|

ラベル(色)指定のあるタグを作成する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# macOS14版
# 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 appFileManager to refMe's NSFileManager's defaultManager()

#######################################
##設定項目 
#######################################
##追加するタグの名前
set strTagName to ("名称未設定新規タグ") as text
##ラベル番号=色ね
(*
0:ラベル無し
1:グレー
2:グリーン
3:パープル
4:ブルー
5:イエロー
6:レッド
7:オレンジ
*)
set numLabelNo to 6 as integer


###実行
log doMakeTagName(strTagName, numLabelNo)



#######################################
##ファンクション
#######################################
to doMakeTagName(argTagNameText, argLabeClolorNo)
  
  set strName to argTagNameText as text
  set numLabelNo to argLabeClolorNo as integer
  set appFileManager to refMe's NSFileManager's defaultManager()
  ###処理するファイル名
  set strFileName to "com.apple.LSSharedFileList.ProjectsItems.sfl3" as text
  ###URLに
  set ocidURLArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidAppSuppDirPathURL to ocidURLArray's firstObject()
  set ocidContainerPathURL to (ocidAppSuppDirPathURL's URLByAppendingPathComponent:("com.apple.sharedfilelist") isDirectory:true)
  set ocidSharedFileListURL to (ocidContainerPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false)
  ###NSDATAに読み込みます
  set ocidPlistData to (refMe's NSData's dataWithContentsOfURL:(ocidSharedFileListURL))
  ### 解凍してDictに Flozenなので値を変更するために 可変に変えます
  #NSKeyedUnarchiver's  OS13までの方式
  # set ocidArchveDict to (refMe's NSKeyedUnarchiver's unarchiveObjectWithData:(ocidPlistData))
  #NSKeyedUnarchiver's  OS14からの方式
  set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:((refMe's NSObject)'s class) fromData:(ocidPlistData) |error|:(reference)
  set ocidArchveDict to (item 1 of listResponse)
  ### 可変Dictにセット
  set ocidArchveDictM to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidArchveDictM's setDictionary:ocidArchveDict)
  #######################################
  ### items の処理
  #######################################
  ### items のArrayを取り出して Flozenなので値を変更するために 可変に変えます
  set ocidItemsArray to (ocidArchveDictM's objectForKey:("items"))
  ### 項目入替用のArray 
  set ocidItemsArrayM to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
(ocidItemsArrayM's setArray:ocidItemsArray)
  ##################
  ###itemsの数だけ繰り返し
  repeat with itemsArrayDict in ocidItemsArray
    #
    set strItemName to (itemsArrayDict's objectForKey:("Name")) as text
    if strItemName is strName then
      ###値があった場合
      set boolChkTagName to true as boolean
      exit repeat
    else
      ###なかった場合
      set boolChkTagName to false as boolean
    end if
  end repeat
log "タグの有無:" & boolChkTagName
  #######################################
  ### タグがない場合は作る
  #######################################
  if boolChkTagName is false then
    try
      set strAgentPath to "/System/Library/LaunchAgents/com.apple.coreservices.sharedfilelistd.plist"
      set strCommandText to ("/bin/launchctl unload -w \"" & strAgentPath & "\"") as text
do shell script strCommandText
    end try
    try
      set strAgentPath to "/System/Library/LaunchAgents/com.apple.cfprefsd.xpc.agent.plist"
      set strCommandText to ("/bin/launchctl unload -w \"" & strAgentPath & "\"") as text
do shell script strCommandText
    end try
    ### 項目追加用のDict
    set ocidAddProkectDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
    set ocidCustomPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
    ################################CustomItemProperties
    set ocidTrue to refMe's NSNumber's numberWithBool:(true)
ocidCustomPropertiesDict's setValue:(ocidTrue) forKey:("kLSSharedTagFileListItemPinned")
    ##表示させたい場合はここをocidFalseに
    set ocidTrue to refMe's NSNumber's numberWithBool:(true)
ocidCustomPropertiesDict's setValue:(ocidTrue) forKey:("com.apple.LSSharedFileList.ItemIsHidden")
    ##
    set ocidLabelNo to refMe's NSNumber's numberWithInteger:(numLabelNo)
ocidCustomPropertiesDict's setValue:(ocidLabelNo) forKey:("kLSSharedTagFileListItemLabel")
    ##CustomItemPropertiesでDictを追加
ocidAddProkectDict's setObject:(ocidCustomPropertiesDict) forKey:("CustomItemProperties")
    #######################################
    ### Bookmarkデータ生成
    #######################################
    (*
URL形式で
x-apple-findertag:%エンコードされたタグの名前
の形式になります
*)
    set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
ocidURLComponents's setScheme:("x-apple-findertag")
ocidURLComponents's setPath:(strName)
    set ocidTagURL to ocidURLComponents's |URL|()
    set listBookMarkData to (ocidTagURL's bookmarkDataWithOptions:(11) includingResourceValuesForKeys:({missing value}) relativeToURL:(missing value) |error|:(reference))
    set ocidBookMarkData to item 1 of listBookMarkData
ocidAddProkectDict's setObject:(ocidBookMarkData) forKey:("Bookmark")
    ##
    set ocidTagName to refMe's NSString's stringWithString:(strName)
ocidAddProkectDict's setObject:(ocidTagName) forKey:("Name")
    ##
    set ocidUUID to refMe's NSUUID's alloc()'s init()
    set ocidUUIDString to ocidUUID's UUIDString()
ocidAddProkectDict's setValue:(ocidUUIDString) forKey:("uuid")
    ##
    set ocidVisibility to refMe's NSNumber's numberWithInteger:(0)
ocidAddProkectDict's setValue:(ocidVisibility) forKey:("visibility")
    ##itemsのArrayに追加
ocidItemsArrayM's addObject:(ocidAddProkectDict)
    ### RootにItemsを追加
(ocidArchveDictM's setObject:(ocidItemsArrayM) forKey:("items"))
    #######################################
    ### 値が新しくなった解凍済みDictをアーカイブする
    #######################################
    ##NSKeyedArchiverに戻す OS13までの形式
    # set listSaveData to (refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(true) |error|:(reference))
    ##NSKeyedArchiver OS14からの形式
    set listSaveData to refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(false) |error|:(reference)
    set ocidSaveData to item 1 of listSaveData
    ###########
    ##保存
    set listDone to ocidSaveData's writeToURL:(ocidSharedFileListURL) options:0 |error|:(reference)
    ###########
    ###リロード
    ###########
    try
      set strAgentPath to "/System/Library/LaunchAgents/com.apple.coreservices.sharedfilelistd.plist"
      set strCommandText to ("/bin/launchctl load -w \"" & strAgentPath & "\"") as text
do shell script strCommandText
    end try
    try
      set strAgentPath to "/System/Library/LaunchAgents/com.apple.cfprefsd.xpc.agent.plist"
      set strCommandText to ("/bin/launchctl load -w \"" & strAgentPath & "\"") as text
do shell script strCommandText
    end try
delay 1
    try
do shell script "/usr/bin/killall cfprefsd"
    end try
    try
do shell script "/usr/bin/killall sharedfilelistd"
    end try
    
    
  else
    try
do shell script "/usr/bin/killall cfprefsd"
    end try
    
    try
do shell script "/usr/bin/killall sharedfilelistd"
    end try
  end if
  
  
  
  
end doMakeTagName


|

[LSSharedFileList.ProjectsItems]色指定付きのタグを作る(macOS14 sfl3対応)

Screen_20230809_11_58_19_20240312193201

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
Finder>>設定>>タグに項目を追加します
macOS14以降のsfl3版です
*)
----+----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 strName to ("Windows非互換文字") as text
###ラベルカラー番号
set numLabelNo to (5) as integer
(* ラベルカラー番号 Finderラベル番号の逆順なので留意ください
0 なし
1 グレイ
2 グリーン
3 パープル
4 ブルー
5 イエロー
6 レッド
7 オレンジ
*)

#######################################
##NSdataに読み込み Keyを解凍する
#######################################
set appFileManager to refMe's NSFileManager's defaultManager()
###処理するファイル名
set strFileName to "com.apple.LSSharedFileList.ProjectsItems.sfl3" as text
###URLに
set ocidURLArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidAppSuppDirPathURL to ocidURLArray's firstObject()
set ocidContainerPathURL to (ocidAppSuppDirPathURL's URLByAppendingPathComponent:("com.apple.sharedfilelist") isDirectory:true)
set ocidSharedFileListURL to (ocidContainerPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false)
###NSDATAに読み込みます
set ocidPlistData to (refMe's NSData's dataWithContentsOfURL:(ocidSharedFileListURL))
### 解凍してDictに Flozenなので値を変更するために 可変に変えます
#NSKeyedUnarchiver's  OS13までの方式
# set ocidArchveDict to (refMe's NSKeyedUnarchiver's unarchiveObjectWithData:(ocidPlistData))
#NSKeyedUnarchiver's  OS14からの方式
set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:((refMe's NSObject)'s class) fromData:(ocidPlistData) |error|:(reference)
set ocidArchveDict to (item 1 of listResponse)
### 可変Dictにセット
set ocidArchveDictM to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidArchveDictM's setDictionary:ocidArchveDict)

#######################################
### items の処理
#######################################
### items のArrayを取り出して Flozenなので値を変更するために 可変に変えます
set ocidItemsArray to (ocidArchveDictM's objectForKey:("items"))
### 項目入替用のArray 
set ocidItemsArrayM to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
(ocidItemsArrayM's setArray:ocidItemsArray)
set numCntArrayItem to count of ocidItemsArray
#######################################
### 値がすでにあるか?確認
set boolChkTagName to (missing value)
##################
###itemsの数だけ繰り返し
repeat with itemsArrayDict in ocidItemsArrayM
  ####
  set strItemName to (itemsArrayDict's objectForKey:("Name")) as text
  if strItemName is strName then
    ###値があった場合
    set boolChkTagName to true as boolean
    exit repeat
  else
    ###なかった場合
    set boolChkTagName to false as boolean
  end if
end repeat
#######################################
### 本処理項目の追加
#######################################
### 項目追加用のDict
set ocidAddProkectDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set ocidCustomPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###すでにあるか?判定でなけば追加する
if boolChkTagName is false then
  ################################CustomItemProperties
  set ocidTrue to refMe's NSNumber's numberWithBool:(true)
ocidCustomPropertiesDict's setValue:(ocidTrue) forKey:("kLSSharedTagFileListItemPinned")
  ##表示させたい場合はここをocidFalseに
  set ocidTrue to refMe's NSNumber's numberWithBool:(true)
ocidCustomPropertiesDict's setValue:(ocidTrue) forKey:("com.apple.LSSharedFileList.ItemIsHidden")
  (* #### LabelNumber
0:ラベル無し
1:グレー
2:グリーン
3:パープル
4:ブルー
5:イエロー
6:レッド
7:オレンジ
*)
  set numLabelNo to 3 as integer
  #
  set ocidLabelNo to refMe's NSNumber's numberWithInteger:(numLabelNo)
ocidCustomPropertiesDict's setValue:(ocidLabelNo) forKey:("kLSSharedTagFileListItemLabel")
  ##CustomItemPropertiesでDictを追加
ocidAddProkectDict's setObject:(ocidCustomPropertiesDict) forKey:("CustomItemProperties")
  ################################ 追加用のDict root
  (* Bookmarkデータ生成
URL形式で
x-apple-findertag:%エンコードされたタグの名前
の形式になります
*)
  set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
ocidURLComponents's setScheme:("x-apple-findertag")
ocidURLComponents's setPath:(strName)
  set ocidTagURL to ocidURLComponents's |URL|()
  set listBookMarkData to (ocidTagURL's bookmarkDataWithOptions:(11) includingResourceValuesForKeys:({missing value}) relativeToURL:(missing value) |error|:(reference))
  set ocidBookMarkData to item 1 of listBookMarkData
ocidAddProkectDict's setObject:(ocidBookMarkData) forKey:("Bookmark")
  ##
  set ocidTagName to refMe's NSString's stringWithString:(strName)
ocidAddProkectDict's setObject:(ocidTagName) forKey:("Name")
  ##
  set ocidUUID to refMe's NSUUID's alloc()'s init()
  set ocidUUIDString to ocidUUID's UUIDString()
ocidAddProkectDict's setValue:(ocidUUIDString) forKey:("uuid")
  ##
  set ocidVisibility to refMe's NSNumber's numberWithInteger:(0)
ocidAddProkectDict's setValue:(ocidVisibility) forKey:("visibility")
  ##itemsのArrayに追加
ocidItemsArrayM's addObject:(ocidAddProkectDict)
end if
### RootにItemsを追加
(ocidArchveDictM's setObject:(ocidItemsArrayM) forKey:("items"))

#######################################
### 値が新しくなった解凍済みDictをアーカイブする
#######################################
##NSKeyedArchiverに戻す OS13までの形式
# set listSaveData to (refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(true) |error|:(reference))
##NSKeyedArchiver OS14からの形式
set listSaveData to refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(false) |error|:(reference)
set ocidSaveData to item 1 of listSaveData

#######################################
### データを上書き保存する
#######################################
##保存
set listDone to ocidSaveData's writeToURL:(ocidSharedFileListURL) options:0 |error|:(reference)

#######################################
### リロード
#######################################
try
do shell script "/usr/bin/killall sharedfilelistd"
on error
  set strAgentPath to "/System/Library/LaunchAgents/com.apple.coreservices.sharedfilelistd.plist"
  set strCommandText to ("/bin/launchctl stop -w \"" & strAgentPath & "\"")
  try
do shell script strCommandText
  end try
  set strCommandText to ("/bin/launchctl start -w \"" & strAgentPath & "\"")
  try
do shell script strCommandText
  end try
end try
delay 0.5
do shell script "/usr/bin/killall Finder"

return


|

[LSSharedFileList]サーバーへ接続のよく使うサーバーを追加

20240312070809_972x462

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
サーバーに接続の
よく使うサーバーを追加します
macOS14以降のsfl3版です
ゲストは
SMBなら
smb://guest:guest@192.168.0.XX:445/Shared
AFPなら
afp://;AUTH=No User Authent@192.168.0.XX:548/Shared

*)
----+----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

#############################################
###設定項目
#############################################
###追加するサーバーURL
set strSetURL to ("smb://guest:guest@192.168.0.1:445/Shared") as text



#######################################
##NSdataに読み込み Keyを解凍する
#######################################
set appFileManager to refMe's NSFileManager's defaultManager()
###処理するファイル名
set strFileName to "com.apple.LSSharedFileList.FavoriteServers.sfl3" as text
###URLに
set ocidURLArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidAppSuppDirPathURL to ocidURLArray's firstObject()
set ocidContainerPathURL to (ocidAppSuppDirPathURL's URLByAppendingPathComponent:("com.apple.sharedfilelist") isDirectory:true)
set ocidSharedFileListURL to (ocidContainerPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false)
###NSDATAに読み込みます
set ocidPlistData to (refMe's NSData's dataWithContentsOfURL:(ocidSharedFileListURL))
### 解凍してDictに Flozenなので値を変更するために 可変に変えます
#NSKeyedUnarchiver's  OS13までの方式
# set ocidArchveDict to (refMe's NSKeyedUnarchiver's unarchiveObjectWithData:(ocidPlistData))
#NSKeyedUnarchiver's  OS14からの方式
set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:((refMe's NSObject)'s class) fromData:(ocidPlistData) |error|:(reference)
set ocidArchveDict to (item 1 of listResponse)
### 可変Dictにセット
set ocidArchveDictM to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidArchveDictM's setDictionary:ocidArchveDict)

#######################################
### items の処理
#######################################
### items のArrayを取り出して Flozenなので値を変更するために 可変に変えます
set ocidItemsArray to (ocidArchveDictM's objectForKey:("items"))
### 項目入替用のArray 
set ocidItemsArrayM to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
(ocidItemsArrayM's setArray:ocidItemsArray)
set numCntArrayItem to count of ocidItemsArray
#######################################
### 値がすでにあるか?確認
set boolChkTagName to (missing value)
##################
###itemsの数だけ繰り返し
repeat with itemsArrayDict in ocidItemsArrayM
  ####
  set strItemName to (itemsArrayDict's objectForKey:("Name")) as text
  if strItemName is strSetURL then
    ###値があった場合
    set boolChkTagName to true as boolean
    exit repeat
  else
    ###なかった場合
    set boolChkTagName to false as boolean
  end if
end repeat

#######################################
### 本処理項目の追加
#######################################

###なければ追加
if boolChkTagName is false then
  ######## 【1】項目追加用のDict
  set ocidAddDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  ######## CustomItemProperties
  #【2】CustomItemProperties用のDICT
  set ocidCustomPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  #
  set ocidSetValue to refMe's NSString's stringWithString:("srvr")
ocidCustomPropertiesDict's setValue:(ocidSetValue) forKey:("com.apple.LSSharedFileList.OverrideIcon.OSType")
  #【1】のDICTに追加
ocidAddDict's setObject:(ocidCustomPropertiesDict) forKey:("CustomItemProperties")
  ######## 追加用のDict root
  ## Name
  set ocidSetValue to refMe's NSString's stringWithString:(strSetURL)
ocidAddDict's setObject:(ocidSetValue) forKey:("Name")
  ## uuid
  set ocidUUID to refMe's NSUUID's alloc()'s init()
  set ocidUUIDString to ocidUUID's UUIDString()
ocidAddDict's setValue:(ocidUUIDString) forKey:("uuid")
  ## visibility
  set ocidVisibility to refMe's NSNumber's numberWithInteger:(0)
ocidAddDict's setValue:(ocidVisibility) forKey:("visibility")
  ## Bookmark
  set ocidURLString to refMe's NSString's stringWithString:(strSetURL)
  set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
  set listBookMarkData to (ocidURL's bookmarkDataWithOptions:(11) includingResourceValuesForKeys:({missing value}) relativeToURL:(missing value) |error|:(reference))
  set ocidBookMarkData to (item 1 of listBookMarkData)
ocidAddDict's setObject:(ocidBookMarkData) forKey:("Bookmark")
  
  ##itemsのArrayに追加
ocidItemsArrayM's addObject:(ocidAddDict)
end if
### RootにItemsを追加
(ocidArchveDictM's setObject:(ocidItemsArrayM) forKey:("items"))

#######################################
### 値が新しくなった解凍済みDictをアーカイブする
#######################################
##NSKeyedArchiverに戻す OS13までの形式
# set listSaveData to (refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(true) |error|:(reference))
##NSKeyedArchiver OS14からの形式
set listSaveData to refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(false) |error|:(reference)
set ocidSaveData to item 1 of listSaveData

#######################################
### データを上書き保存する
#######################################
##保存
set listDone to ocidSaveData's writeToURL:(ocidSharedFileListURL) options:0 |error|:(reference)

#######################################
### リロード
#######################################
try
do shell script "/usr/bin/killall sharedfilelistd"
on error
  set strAgentPath to "/System/Library/LaunchAgents/com.apple.coreservices.sharedfilelistd.plist"
  set strCommandText to ("/bin/launchctl stop -w \"" & strAgentPath & "\"")
  try
do shell script strCommandText
  end try
  set strCommandText to ("/bin/launchctl start -w \"" & strAgentPath & "\"")
  try
do shell script strCommandText
  end try
end try
delay 0.5
## do shell script "/usr/bin/killall Finder"

return


|

[LSSharedFileList]Finderサイドバーのよく使う項目を追加する

20240312072816_1272x780

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
サーバーに接続の
よく使うサーバーを追加します
macOS14以降のsfl3版です
AirDrop
nwnode://domain-AirDrop
一部個別のスキームがあります

*)
----+----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 strAddDirPath to ("~/Downloads") as text

###
set ocidAddDirPathStr to refMe's NSString's stringWithString:(strAddDirPath)
set ocidAddDirPath to ocidAddDirPathStr's stringByStandardizingPath()
set ocidAddDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAddDirPath) isDirectory:false)
set strAddDirPath to ocidAddDirPathURL's absoluteString() as text

#######################################
##NSdataに読み込み Keyを解凍する
#######################################
set appFileManager to refMe's NSFileManager's defaultManager()
###処理するファイル名
set strFileName to "com.apple.LSSharedFileList.FavoriteItems.sfl3" as text
###URLに
set ocidURLArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidAppSuppDirPathURL to ocidURLArray's firstObject()
set ocidContainerPathURL to (ocidAppSuppDirPathURL's URLByAppendingPathComponent:("com.apple.sharedfilelist") isDirectory:true)
set ocidSharedFileListURL to (ocidContainerPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false)
###NSDATAに読み込みます
set ocidPlistData to (refMe's NSData's dataWithContentsOfURL:(ocidSharedFileListURL))
### 解凍してDictに Flozenなので値を変更するために 可変に変えます
#NSKeyedUnarchiver's  OS13までの方式
# set ocidArchveDict to (refMe's NSKeyedUnarchiver's unarchiveObjectWithData:(ocidPlistData))
#NSKeyedUnarchiver's  OS14からの方式
set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:((refMe's NSObject)'s class) fromData:(ocidPlistData) |error|:(reference)
set ocidArchveDict to (item 1 of listResponse)
### 可変Dictにセット
set ocidArchveDictM to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidArchveDictM's setDictionary:ocidArchveDict)

#######################################
### items の処理
#######################################
### items のArrayを取り出して Flozenなので値を変更するために 可変に変えます
set ocidItemsArray to (ocidArchveDictM's objectForKey:("items"))
### 項目入替用のArray 
set ocidItemsArrayM to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
(ocidItemsArrayM's setArray:ocidItemsArray)
set numCntArrayItem to count of ocidItemsArray
#######################################
### 値がすでにあるか?確認
set boolChkTagName to (missing value)
##################
###itemsの数だけ繰り返し
repeat with itemsArrayDict in ocidItemsArrayM
  ####
  set ocidBookMarkData to (itemsArrayDict's objectForKey:("Bookmark"))
  #BOOKMarkデータの解凍
  set listResponse to (refMe's NSURL's URLByResolvingBookmarkData:(ocidBookMarkData) options:(refMe's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:(missing value) |error|:(reference))
  set ocidBookMarkURL to (item 1 of listResponse)
  set strBookMarkPath to ocidBookMarkURL's absoluteString() as text
  ##
  if strAddDirPath is strBookMarkPath then
    ###値があった場合
    set boolChkTagName to true as boolean
    exit repeat
  else
    ###なかった場合
    set boolChkTagName to false as boolean
  end if
end repeat

#######################################
### 本処理項目の追加
#######################################

###なければ追加
if boolChkTagName is false then
  ######## 【1】項目追加用のDict
  set ocidAddDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  ######## CustomItemProperties
  #【2】CustomItemProperties用のDICT
  set ocidCustomPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  # 0=true 1=false
  set ocidSetValue to (refMe's NSNumber's numberWithInteger:1)
ocidCustomPropertiesDict's setValue:(ocidSetValue) forKey:("com.apple.LSSharedFileList.ItemIsHidden")
  # 0=true 1=false
  set ocidSetValue to (refMe's NSNumber's numberWithInteger:0)
ocidCustomPropertiesDict's setValue:(ocidSetValue) forKey:("com.apple.finder.dontshowonreappearance")
  
  #【1】のDICTに追加
ocidAddDict's setObject:(ocidCustomPropertiesDict) forKey:("CustomItemProperties")
  ######## 追加用のDict root
  ## uuid
  set ocidUUID to refMe's NSUUID's alloc()'s init()
  set ocidUUIDString to ocidUUID's UUIDString()
ocidAddDict's setValue:(ocidUUIDString) forKey:("uuid")
  ## visibility
  set ocidVisibility to refMe's NSNumber's numberWithInteger:(0)
ocidAddDict's setValue:(ocidVisibility) forKey:("visibility")
  ## Bookmark
  set listBookMarkData to (ocidAddDirPathURL's bookmarkDataWithOptions:(11) includingResourceValuesForKeys:({missing value}) relativeToURL:(missing value) |error|:(reference))
  set ocidBookMarkData to (item 1 of listBookMarkData)
ocidAddDict's setObject:(ocidBookMarkData) forKey:("Bookmark")
  
  ##itemsのArrayに追加
ocidItemsArrayM's addObject:(ocidAddDict)
end if
### RootにItemsを追加
(ocidArchveDictM's setObject:(ocidItemsArrayM) forKey:("items"))

#######################################
### 値が新しくなった解凍済みDictをアーカイブする
#######################################
##NSKeyedArchiverに戻す OS13までの形式
# set listSaveData to (refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(true) |error|:(reference))
##NSKeyedArchiver OS14からの形式
set listSaveData to refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidArchveDictM) requiringSecureCoding:(false) |error|:(reference)
set ocidSaveData to item 1 of listSaveData

#######################################
### データを上書き保存する
#######################################
##保存
set listDone to ocidSaveData's writeToURL:(ocidSharedFileListURL) options:0 |error|:(reference)

#######################################
### リロード
#######################################
try
do shell script "/usr/bin/killall sharedfilelistd"
on error
  set strAgentPath to "/System/Library/LaunchAgents/com.apple.coreservices.sharedfilelistd.plist"
  set strCommandText to ("/bin/launchctl stop -w \"" & strAgentPath & "\"")
  try
do shell script strCommandText
  end try
  set strCommandText to ("/bin/launchctl start -w \"" & strAgentPath & "\"")
  try
do shell script strCommandText
  end try
end try
delay 0.5
## do shell script "/usr/bin/killall Finder"

return


|

[NSKeyedArchiver]plistをsfl3形式にKeyedArchiveする


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

#!/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 appFileManager to refMe's NSFileManager's defaultManager()

###################################
#####ファイル選択ダイアログ
###################################
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 aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
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 listUTI to {"com.apple.property-list"} as list
set aliasFilePath to (choose file with prompt "plistファイルを選んでください" 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)
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
set ocidPrefixName to ocidBaseFilePathURL's lastPathComponent()
###################################
#####保存先ダイアログ
###################################
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子変える場合
set strFileExtension to "sfl3"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & "." & strFileExtension) as text
set strPromptText to "名前を決めてください"
set strMesText to "名前を決めてください"
####
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias

####実在しない『はず』なのでas «class furl»で
set aliasSaveFilePath to (choose file name strMesText default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
####UNIXパス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
####ドキュメントのパスをNSString
set ocidSaveFilePath to refMe's NSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURLに
set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:(strFileExtension)
end if

###################################
#####本処理
###################################
# DICTに読み込んで
set listResponse to refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
set ocidReadData to (item 1 of listResponse)
#アーカイブする
#
set listResponse to refMe's NSKeyedArchiver's archivedDataWithRootObject:(ocidReadData) requiringSecureCoding:(false) |error|:(reference)
set ocidSfl3Data to (item 1 of listResponse)
if ocidSfl3Data = (missing value) then
return "アーカイブに失敗しました"
end if


#ファイル保存
set listDone to ocidSfl3Data's writeToURL:(ocidSaveFilePathURL) options:0 |error|:(reference)



|

[基本]NSKeyedArchiver

1:NSKeyedArchiverアーカイブ
2:NSKeyedUnarchiverアン・アーカイブ(ここでは、解凍と呼ぶことにします)



1:NSKeyedArchiverアーカイブ
[基本 NSKeyedArchiver] 1:NSKeyedArchiver アーカイブする
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-e62bae.html


2:NSKeyedUnarchiver解凍
[基本 NSKeyedArchiver] 2:NSKeyedUnarchiver アン・アーカイブ解凍する
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-c324cb.html

|

[基本 NSKeyedArchiver] 2:NSKeyedUnarchiver アン・アーカイブ解凍する

NSKeyedArchiverされた こんなデータを Screen-20230918-182132 解凍してキーと値のPLIST形式にします Screen-20230918-182015

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

#!/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

####【1】読み込むPLISTのパス
#アーカイブされたPlistのパス
set strFilePath to "~/Desktop/com.apple.archiveutility.plist" 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)

####【2】PLISTのデータをNSDATAに読み込み
set ocidPlistDictData to refMe's NSData's dataWithContentsOfURL:(ocidFilePathURL)
####【3】解凍する
set ocidPlistArchiver to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:(refMe's NSDictionary's class) fromData:(ocidPlistDictData) |error|:(missing value)
log ocidPlistArchiver as record

####【4】出力ファイルパス デスクトップ ファイルは削除してもOKです
set strSaveFilePath to "~/Desktop/com.apple.archiveutility.Unarchiver.plist" as text
set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)

####【5】保存
set boolDone to ocidPlistArchiver's writeToURL:(ocidSaveFilePathURL) atomically:yes





|

その他のカテゴリー

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