запись / чтение одной переменной в applecript - PullRequest
1 голос
/ 12 сентября 2011

Я только начинаю с applecript в xcode, и в настоящее время у меня есть приложение, которое запрашивает местоположение папки, а затем создает структуру папок.

on buttonClick_(sender)
    set theLocation to choose folder with prompt "Where to save your project?"
    tell application "Finder"
        set newFolder to make new folder at theLocation with properties {name:(theTextField's stringValue() as string)}
        set fontsFolder to make new folder at newFolder with properties {name:"fonts"}
        set jpgFolder to make new folder at newFolder with properties {name:"jpg-pdf"}
        set mainFolder to make new folder at newFolder with properties {name:"main"}
        set printFolder to make new folder at mainFolder with properties {name:"• for printer"}
        set refverFolder to make new folder at newFolder with properties {name:"ref_ver"}
        set supportFolder to make new folder at newFolder with properties {name:"support"}
    end tell
    quit
end buttonClick_

Теперь я пытаюсь заставить приложение взять псевдоним папки "theLocation" и сохранить его, поэтому при следующем запуске приложения он автоматически выбирает эту папку в качестве места сохранения без добавления.Я понимаю логику, которая пойдет на это, но я не могу понять, как хранить / читать информацию.Я пробовал учебники по записи в info.plist, но ни один из них не работал.Мне не хватает основной информации о том, как работает appleScript?

Спасибо

** Редактировать

script New_ProjectAppDelegate
property parent : class "NSObject"

property theTextField : missing value
property theLocation : ""

on applicationWillFinishLaunching_(aNotification)
    tell standardUserDefaults() of current application's NSUserDefaults
        registerDefaults_({theLocation:theLocation}) -- register the starting user default key:value items
        set theLocation to objectForKey_("theLocation") as text -- read any previously saved items (which will update the values)
    end tell
end applicationWillFinishLaunching_

on buttonClick_(sender)
    if theLocation is "" then
        set theLocation to choose folder with prompt "Where to save your project?"
        tell standardUserDefaults() of current application's NSUserDefaults
            setObject_forKey_(theLocation, "theLocation") -- update the default items
        end tell
    else
        set theLocation to theLocation as text
        --display dialog theLocation as text
    end if
    tell application "Finder"
        set newFolder to make new folder at theLocation with properties {name:(theTextField's stringValue() as string)}
        set fontsFolder to make new folder at newFolder with properties {name:"fonts"}
        set jpgFolder to make new folder at newFolder with properties {name:"jpg-pdf"}
        set mainFolder to make new folder at newFolder with properties {name:"main"}
        set printFolder to make new folder at mainFolder with properties {name:"• for printer"}
        set refverFolder to make new folder at newFolder with properties {name:"ref_ver"}
        set supportFolder to make new folder at newFolder with properties {name:"support"}
    end tell
    quit
end buttonClick_


on applicationShouldTerminate_(sender)
    tell standardUserDefaults() of current application's NSUserDefaults
        setObject_forKey_(theLocation, "theLocation") -- update the default items
    end tell
    return current application's NSTerminateNow
end applicationShouldTerminate_

end script

Ответы [ 2 ]

2 голосов
/ 12 сентября 2011

Свойства не являются постоянными в сценариях XCode, но вы можете использовать систему по умолчанию .Чтобы использовать значения по умолчанию, вы регистрируете некоторые начальные значения при запуске приложения, затем считываете значения по умолчанию (которые перезаписывают зарегистрированные значения, если они были сохранены ранее), а когда ваше приложение выходит, сохраняют новые значения - например:

property theLocation : "" -- this will be the (text) folder path

on applicationWillFinishLaunching_(aNotification)
    tell standardUserDefaults() of current application's NSUserDefaults
        registerDefaults_({theLocation:theLocation}) -- register the starting user default key:value items
        set theLocation to objectForKey_("theLocation") as text -- read any previously saved items (which will update the values)
    end tell
    -- other initialization stuff
end applicationWillFinishLaunching_

on buttonClick_(sender)
    if theLocation is "" then
        set theLocation to choose folder with prompt "Where to save your project?"
        set theLocation to theLocation as text
    end if
    -- display dialog theLocation
    tell application "Finder"
        -- create folder structure
    end tell
    quit
end buttonClick_

on applicationShouldTerminate_(sender)
    tell standardUserDefaults() of current application's NSUserDefaults
        setObject_forKey_(theLocation, "theLocation") -- update the default items
    end tell
    return current application's NSTerminateNow
end applicationShouldTerminate_
0 голосов
/ 12 сентября 2011

Вы можете определить свойство в начале вашего скрипта, которое позволит вам хранить информацию даже после выхода из скрипта ...

property theLocation : null
if theLocation is null then set theLocation to (choose folder with prompt "Where to save your project?"
...

Скрипт будет хранить псевдоним в "theLocation" для будущего использования. Однако при сохранении или перекомпиляции сценария свойство вернется к исходному значению.

...