Изменить AppleScript PList - PullRequest
       10

Изменить AppleScript PList

0 голосов
/ 08 ноября 2019

Не удалось найти много документации по файлам PList, я думаю, что мне не хватает некоторого синтаксиса.

Цель - запустить скрипт и записать записи пользователя в a. plist, чтобы эти значения могли быть возвращены, даже если сценарий завершается. Более поздняя цель - вывести эти значения по электронной почте или через Google Suite. Имел возможность создавать, изменять и вызывать отдельные значения, но добавление отдельных элементов в списки и последующий вызов всего списка (или даже одного значения) пока не работает.

set CaptureFolder to (choose folder with prompt "Select Capture Folder")
set PListString to (CaptureFolder & "Capture.plist") as text

try
    set PListAlias to PListString as alias
    set BuildPList to false
on error
    set BuildPList to true
end try

if BuildPList is false then
    tell application "System Events"
        set theParentDictionary to make new property list item with properties {kind:record}
        set thePListFile to PListString
        set thePListDir to property list file thePListFile
    end tell
else
    set PListPath to POSIX path of PListString
    tell application "System Events"
        set theParentDictionary to make new property list item with properties {kind:record}
        set thePListFile to PListString
        set thePListFileDir to make new property list file with properties {contents:theParentDictionary, name:thePListFile}
        tell property list items of thePListFileDir
            make new property list item at end with properties {kind:boolean, name:"booleanKey", value:true}
            make new property list item at end with properties {kind:date, name:"dateKey", value:current date}
            make new property list item at end with properties {kind:list, name:"SKUlist", value:{}}
            make new property list item at end with properties {kind:list, name:"unknownDeptlist"}
            make new property list item at end with properties {kind:list, name:"Folderlist"}
            make new property list item at end with properties {kind:number, name:"FolderCounter", value:0}
            make new property list item at end with properties {kind:record, name:"recordKey"}
            make new property list item at end with properties {kind:string, name:"stringKey", value:"string value"}
        end tell
    end tell
end if

if BuildPList is false then
    tell application "System Events"
        tell property list file thePListFile
            set FolderNumber to value of property list item "FolderCounter"

            # This keeps erroring out
            set PseudoSKU to the value of item -1 of property list item "SKUList"
            #

        end tell
    end tell
else
    set FolderNumber to {0}
end if

# Sample Loops
repeat 5 times

    set FolderNumber to (FolderNumber + 1)
    set FolderNumberString to FolderNumber as string
    set PseudoSKU to ("SKU_" & FolderNumber)

    display dialog (FolderNumberString & " " & PseudoSKU) --

    tell application "System Events"
        tell property list file thePListFile
            set value of property list item "FolderCounter" to FolderNumber

            # Same item, but in reverse
            set array of property list item "SKUList" to PseudoSKU
            #

        end tell
    end tell

end repeat

''«

1 Ответ

0 голосов
/ 09 ноября 2019

Системные события * Комплект списка свойств 1002 * может немного запутать, но вы можете использовать обычную запись AppleScript для управления отдельными элементами и просто использовать Системные события (или даже некоторые AppleScriptObjC). ) для чтения и записи файла списка свойств:

set captureFolder to (choose folder with prompt "Select Capture Folder")
set plistPath to (captureFolder & "Capture.plist") as text

try -- test to see if plist file needs to be created
    plistPath as alias
    set buildPlist to false
on error errmess -- no file
    log errmess
    set buildPlist to true
end try

if buildPlist then -- make new property list file
    # create default record
    set dateString to (current date) as «class isot» as string -- date format that can be stored
    set plistRecord to {Label:"Testing", Container:(POSIX path of plistPath), UnknownDeptlist:{}, ModificationDate:dateString, FolderCounter:0, Folderlist:{}, SKUlist:{}}
    tell application "System Events"
        set parentDictionary to make new property list item with properties {kind:record} -- base
        set plistFile to make new property list file with properties {contents:parentDictionary, name:plistPath}
        tell property list items of plistFile
            make new property list item at end with properties {kind:record, value:plistRecord}
        end tell
    end tell
else -- use existing
    tell application "System Events"
        set plistFile to property list file plistPath
        set plistRecord to value of plistFile
    end tell
end if

# work with record items as needed
set plistRecord's ModificationDate to (current date) as «class isot» as string
repeat 5 times
    set plistRecord's FolderCounter to (plistRecord's FolderCounter) + 1
    set pseudoSKU to ("SKU_" & plistRecord's FolderCounter)
    set end of plistRecord's SKUlist to pseudoSKU
end repeat

# update file
tell application "System Events" to set value of plistFile to plistRecord
...