Kevin
Kevin

Reputation: 1106

Xcode : read plist data and display them as text

I totally forgot about Xcode AppleScript Application.

Basically, the application will read value from plist file.

My applescript to read this variable is

set the plistfile_path to "~/Desktop/example.plist"

tell application "System Events"
    set p_list to property list file (plistfile_path)
    -- read the plist data
    set theNameFromPlist to value of property list item "theName" of p_list
    set theEmailFromPlist to value of property list item "theEmail" of p_list
    set thecreationDateFromPlist to value of property list item "creationDate" of p_list


end tell

now how do I integrate this to Xcode?

Should I go on AppDelegate.applescript and add wherever after "applicationWillFinishLaunching_(aNotification)" this script ?

followed by  property NametextField : theNameFromPlist

I'm probably totally wrong here, just can't think. PS swift will do it if any easier

Upvotes: 0

Views: 968

Answers (2)

vadian
vadian

Reputation: 285200

Mentioning Cocoa classes I actually meant this

set plistFile to POSIX path of (path to desktop) & "example.plist"
set thePlist to (current application's NSDictionary's dictionaryWithContentsOfFile:plistFile) as record
set theNameFromPlist to thePlist's theName
set theEmailFromPlist to thePlist's theEmail

To add new key value pairs and write the plist back to disk add

set thePlist to thePlist & {stringKey:"Foo", booleanKey:false}
set cocoaDictionary to current application's NSDictionary's dictionaryWithDictionary:thePlist
cocoaDictionary's writeToFile:plistFile atomically:true

Upvotes: 1

YeaTheMen
YeaTheMen

Reputation: 1083

Like vadian said in the comments, In Xcode, System Events won't run properly so you can just use the native Cocoa classes but I prefer to use "do shell script" applescript/asobjc command and run the "defaults" command so here is a way to do that when the application will finish launching:

on applicationWillFinishLaunching_(aNotification)
    set plistFile to "~/Desktop/example.plist"
    set theNameFromPlist to do shell script "defaults read " & plistFile & " theName"
    set theEmailFromPlist to do shell script "defaults read " & plistFile & " theEmail"
    set theCreationDateFromPlist to do shell script "defaults read " & plistFile & " creationDate"
end applicationWillFinishLaunching_

And if you are trying to display the name in a TextField on a Window in the Interface Builder you can just add Modify the code to this:

property nameTextField : missing value

on applicationWillFinishLaunching_(aNotification)
    set plistFile to "~/Desktop/example.plist"
    set theNameFromPlist to do shell script "defaults read " & plistFile & " theName"
    set theEmailFromPlist to do shell script "defaults read " & plistFile & " theEmail"
    set theCreationDateFromPlist to do shell script "defaults read " & plistFile & " creationDate"
    tell nameTextField to setStringValue_(theNameFromPlist)
end applicationWillFinishLaunching_

(make sure to connect the property nameTextField to a TextField in the Interface Builder)

hope this Helped!! :D

Upvotes: 1

Related Questions