Getting Property List Values via applescript

I am trying to retrieve values from a .plist to transfer to a .csv

The values will be different with each use, the script I have is

set f to (path to desktop as text) & "results.plist"
tell application "System Events"
	tell (value of (contents of property list file f)) as list
		set |track name| to {}
	end tell
end tell

return |track name|

the .plist is

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>Track Name</key>
		<string>Track 1</string>
		<key>time</key>
		<string>00:01:30;00</string>
	</dict>
	<dict>
		<key>Track Name</key>
		<string>Track 2</string>
		<key>time</key>
		<string>00:01:30;00</string>
	</dict>
	<dict>
		<key>Track Name</key>
		<string>Track 3</string>
		<key>time</key>
		<string>00:01:30;00</string>
	</dict>
	<dict>
		<key>Track Name</key>
		<string>Track 4</string>
		<key>time</key>
		<string>00:01:30;00</string>
	</dict>
	<dict>
		<key>Track Name</key>
		<string>Track 5</string>
		<key>time</key>
		<string>00:01:30;00</string>
	</dict>
	<dict>
		<key>Track Name</key>
		<string>Track 6</string>
		<key>time</key>
		<string>00:01:30;00</string>
	</dict>
	<dict>
		<key>Track Name</key>
		<string>Track 7</string>
		<key>time</key>
		<string>00:02:00;00</string>
	</dict>
</array>
</plist>

If i look at the events when I run it I can see all the values I need but it keeps returning a blank!

I am new to applescript so I know it is probably an easy fix but I have been searching for hours now and keep coming up with a blank, If i try

tell (value of (contents of property list file f)) as record

it returns an error as not being able to make in to type record but it has the info i need!!

any help would be greatly appreciated

Thanks

Ben

Hi,

from your thread in the AppleScriptObjC section I assume you need the code for an ASOC project,
this is a “native” solution, the first line contains the keys, then the values all comma separated


set filePath to POSIX path of (path to desktop) & "test.csv" -- sample path, change it something appropriate
set theArray to arrayController's arrangedObjects() -- arrayController is a property connected to an NSArrayController instance
set csvList to current application's NSMutableArray's array()
csvList's addObject:((theArray's objectAtIndex:0)'s allKeys()'s componentsJoinedByString:",")
repeat with anItem in theArray
	(csvList's addObject:((anItem's allValues())'s componentsJoinedByString:","))
end repeat
set csvText to (csvList's componentsJoinedByString:return)
csvText's writeToFile:filePath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

Stefan I could kiss you… (Don’t worry your safe)

I have been trying to work out a way to export values to a CSV from a NSTableView in an OSX app I am writing using Applescript Obj C in X Code

I kept coming up with nothing and so was trying to do it in a really long way round, writing to a .plist then to a cdv

You good sir have just saved me about 4 steps of nonsense to make it work and hence made me a very happy man.

Thanks

Ben

:slight_smile:

I know I’m being cheeky now

what is the code for importing back in to the table view? if it makes it easier I could have a save as sender and save as a .plist

on ChooseSave:sender
		
		set resultFile to (choose file name with prompt "Choose Save Location" default name "SMPTE Save" default location path to desktop) as text
		set filePath to POSIX path of resultFile & ".plist"
		
		set theArray to ArrayController's arrangedObjects()
        
        theArray's writeToFile:filePath atomically:true
		
	end ChooseSave:

B

:stuck_out_tongue: