Plist処理 基本型
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # PLIST 基本型 |
005 | #com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | ##自分環境がos12なので2.8にしているだけです |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use scripting additions |
012 | |
013 | ############################# |
014 | # |
015 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
016 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
017 | set ocidPlistFilePathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Preferences/■■■■■.plist") isDirectory:(false) |
018 | set ocidPlistFilePath to ocidPlistFilePathURL's |path|() |
019 | #ファイルの有無を確認 |
020 | set boolFileExists to appFileManager's fileExistsAtPath:(ocidPlistFilePath) isDirectory:(false) |
021 | |
022 | if boolFileExists is true then |
023 | #【A】ファイルがある場合 |
024 | #Plist読み込み NSData's |
025 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
026 | set listReadData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL) options:(ocidOption) |error| :(reference) |
027 | if (item 2 of listResponse) = (missing value) then |
028 | set ocidPlistData to (item 1 of listReadData) |
029 | else if (item 2 of listResponse) ≠ (missing value) then |
030 | set strErrorNO to (item 2 of listResponse)'s code() as text |
031 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
032 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
033 | return "エラーしました" & strErrorNO & strErrorMes |
034 | end if |
035 | |
036 | #DICTにする |
037 | set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0) |
038 | set appPlistSerial to refMe's NSPropertyListSerialization |
039 | set ocidOption to refMe's NSPropertyListMutableContainersAndLeaves |
040 | set listPlistDict to appPlistSerial's propertyListWithData:(ocidPlistData) options:(ocidOption) format:(ocidFormat) |error| :(reference) |
041 | if (item 2 of listResponse) = (missing value) then |
042 | set ocidPlistDict to (item 1 of listPlistDict) |
043 | else if (item 2 of listResponse) ≠ (missing value) then |
044 | set strErrorNO to (item 2 of listResponse)'s code() as text |
045 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
046 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
047 | return "エラーしました" & strErrorNO & strErrorMes |
048 | end if |
049 | |
050 | |
051 | else |
052 | #【B】ファイルが無い場合 |
053 | #空のDICTを用意する |
054 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
055 | end if |
056 | ############### |
057 | #ここで処理 |
058 | #ここで処理 |
059 | ############### |
060 | #保存 |
061 | #DICTをPLISTに変換 |
062 | set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0) |
063 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error| :(reference) |
064 | if (item 2 of listResponse) = (missing value) then |
065 | set ocidPlistData to (item 1 of listResponse) |
066 | else if (item 2 of listResponse) ≠ (missing value) then |
067 | set strErrorNO to (item 2 of listResponse)'s code() as text |
068 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
069 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
070 | return "エラーしました" & strErrorNO & strErrorMes |
071 | end if |
072 | |
073 | #保存 |
074 | set ocidOption to (refMe's NSDataWritingAtomic) |
075 | set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error| :(reference) |
076 | if (item 1 of listDone) is true then |
077 | log "正常終了" |
078 | else if (item 1 of listDone) is false then |
079 | set strErrorNO to (item 2 of listResponse)'s code() as text |
080 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
081 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
082 | return "エラーしました" & strErrorNO & strErrorMes |
083 | end if |
084 | #読み込んだDICTの内容をクリア |
085 | set ocidPlistDict to (missing value) |
086 | |
AppleScriptで生成しました |
| 固定リンク