FileMaker and pList

Hi,

I have some modules apps created in Asoc that require to works or not, that user mac address machine is enabled on pList file that reside on web server.
To manage the database where I have my users and related app that user can be launch, I use FileMaker Pro.
The verification is based on every Asoc app that, at launch, read the pList and enable or not the use of the App.

The question is: there is a short way to write the pList using a list of dictionary?
I need to to this from FileMaker menu, so I cannot use Asoc.
I think to something like default write passing a list of records. Any input?

set pLista to {}

tell application "FileMaker Pro Advanced"
	
	set numRecUtenti to count records of table "Users"
	repeat with j from 1 to numRecUtenti
		
		tell record j of table "Users"
			set keyUtente to cell "Key User"
			set nominativo to cell "Fullname"
			set redazione to cell "Area"
			set TCP to cell "TCP"
			set ethernetAddress to cell "Ethernet Address"
		end tell
		
		set numRecApp to count (records of table "Applications" whose cell "Key Application" is keyUtente)
		set listaRecords to (records of table "Applications" whose cell "Key Application" is keyUtente)
		set listaApplicazioni to {}
		repeat with a from 1 to numRecApp
			if numRecApp = 1 then
				copy (item 2 of listaRecords) to end of listaApplicazioni
			else
				copy (item 2 of item a of listaRecords) to end of listaApplicazioni
			end if
		end repeat
		
		copy {nominativo, redazione, TCP, ethernetAddress, listaApplicazioni} to end of pLista
		
	end repeat
	
end tell

-- Generate the pList
set sharedPath to "/Users/Shared/"
set sharedClassicPath to ((POSIX file sharedPath) as string) & "abilitazioni.plist"
set fileRef to open for access file sharedClassicPath with write permission
set eof of fileRef to 0
write "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" & linefeed to fileRef starting at eof as «class utf8»
write "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" & linefeed to fileRef starting at eof as «class utf8»
write "<plist version=\"1.0\">" & linefeed to fileRef starting at eof as «class utf8»
write "<dict>" & linefeed to fileRef starting at eof as «class utf8»
repeat with j from 1 to count pLista
	set recordObject to item j of pLista
	write "<key>" & item 4 of recordObject & "</key>" & linefeed to fileRef starting at eof as «class utf8»
	write "<dict>" & linefeed to fileRef starting at eof as «class utf8»
	write "<key>fullname</key>" & linefeed to fileRef starting at eof as «class utf8»
	write "<string>" & item 1 of recordObject & "</string>" & linefeed to fileRef starting at eof as «class utf8»
	write "<key>area</key>" & linefeed to fileRef starting at eof as «class utf8»
	write "<string>" & item 2 of recordObject & "</string>" & linefeed to fileRef starting at eof as «class utf8»
	write "<key>ip</key>" & linefeed to fileRef starting at eof as «class utf8»
	write "<string>" & item 3 of recordObject & "</string>" & linefeed to fileRef starting at eof as «class utf8»
	write "<key>apps</key>" & linefeed to fileRef starting at eof as «class utf8»
	write "<array>" & linefeed to fileRef starting at eof as «class utf8»
	repeat with a from 1 to count (item 5 of recordObject)
		write "<string>" & item a of (item 5 of recordObject) & "</string>" & linefeed to fileRef starting at eof as «class utf8»
	end repeat
	write "</array>" & linefeed to fileRef starting at eof as «class utf8»
	write "</dict>" & linefeed to fileRef starting at eof as «class utf8»
end repeat
write "</dict>" & linefeed to fileRef starting at eof as «class utf8»
write "</plist>" to fileRef starting at eof as «class utf8»
close access fileRef
display dialog "Export complete." buttons {"OK"} default button 1

There’s no simple way, but it would be faster if you built the full string first, and wrote it all at once.

Hi Shane,

thanks. But using the AS I posted or using do shell?
The script I posted is splitted line by line (as write) for legibility.

Stefano

I don’t see anything to be gained from shell scripting, although maybe I’ve missed something. But you should write only once – you can still do the string building line-by-line for legibility.

Hi,

System Events is able to create property list files quite conveniently, I “translated” your data (but not tested)


set sharedPath to (path to shared documents as text) & "abilitazioni.plist"

tell application "System Events"
	set the rootNode to make new property list item with properties {kind:record}
	set plistFile to make new property list file with properties {contents:rootNode, name:sharedPath}
	
	repeat with j from 1 to count pLista
		set recordObject to item j of pLista
		set dict to make new property list item at end of property list items of contents of plistFile ¬
			with properties {kind:record, name:item 4 of recordObject}
		tell dict
			make new property list item at end of property list items with properties {kind:string, name:"fullname", value:item 1 of recordObject}
			make new property list item at end of property list items with properties {kind:string, name:"area", value:item 2 of recordObject}
			make new property list item at end of property list items with properties {kind:string, name:"ip", value:item 3 of recordObject}
			set array to make new property list item at end of property list items with properties {kind:list, name:"apps"}
			tell array
				repeat with a from 1 to count (item 5 of recordObject)
					make new property list item at end of property list items with properties {kind:string, name:"", value:item a of (item 5 of recordObject)}
				end repeat
			end tell
		end tell
	end repeat
end tell


Thanks Shane, thanks Stefan.

Just for curiosity: using Asoc is possible to write pList using list or records (passing them to Cocoa array) in one shot?

Stefano

Yes, the list and record equivalents in Cocoa (NSArray and NSDictionary) have methods to write plist

That’s exactly what -writeToFile:atomically: does.