ProGrammer
ProGrammer

Reputation: 1031

AppleScript: How do I set a property to a file path?

Background
I am working on an AppleScript application (ASOBJ) through Xcode and frequently access (update/retrieve) various entries from a .plist file that I have created.

Whenever I need to access the data, I use the following command:

set thePropertyListFilePath to POSIX path of (path to resource "Data.plist")
tell application "System Events"
    tell property list file thePropertyListFilePath
        set myAge to value of property list item "userAge" as text
    end tell
end tell

The problem
My project is in development and file names / paths change all too frequently. I want to make a property thePropertyListFilePath : [...] at the top of my project to be able to change the path and name of the file in one place, rather than multiple.
I would like to use property over set as it just works better in my case.

At the top of my code:
property thePropertyListFilePath : POSIX path of (path to resource "Data.plist")

Does not compile because:

Error: Resource not found.

Analysis
I know for a fact that the resource Data.plist is where it is supposed to be and the paths correct because it works fine from inside my top code snippet. Leaving me with the question:

How do I set a property to a file path?

Any insight into why this error is thrown and how to correctly set the property to this (relative) file path is greatly appreciated. I am aware of this question, but it does not present an applicable solution. Cheers.


Edit
I now seem to be getting a related error:

System Events got an error: 'This Mac:Users:myuser:Library:Developer:Xcode:DerivedData:ProjectName:Build:Products:Debug:ProjectName.app:Contents:Resources:Data.plist' is not a property list file. (error -1728)

Some validation needs to take place before app has launched so I wrote my code in the applicationWillFinishLaunching_ section - this shouldn't present any problems.

I know that the file is there and a .plist format so I am thinking it could simply be the tell ... statements around it.
In the past, I used set currentVersion to (current application's class "NSBundle"'s mainBundle()'s objectForInfoDictionaryKey:"CFBundleShortVersionString") as text to access the version number from the Info.plist in one go. Is it possible to use a similar one-liner without the tell statements to retrieve userAge from my own plist file?

Upvotes: 1

Views: 679

Answers (1)

vadian
vadian

Reputation: 285250

Don't do that. Never declare a property with a relative path. The value will set at compile time and will never change. That makes the benefit of the relative path useless.

Declare the property as empty string

property thePropertyListFilePath : ""

and set it in applicationDidFinishLaunching

on applicationDidFinishLaunching_(aNotification)
   set thePropertyListFilePath to POSIX path of (path to resource "Data.plist")
end applicationWillFinishLaunching_

However as you are developing AppleScriptObjC I recommend the Cocoa way (longer but more efficient)

set thePropertyListFilePath to (current application's NSBundle's mainBundle()'s pathForResource_ofType_("Data", "plist")) as text

Upvotes: 1

Related Questions