need to check if plist file exists

G’day

I’ve created a plist file that works ok, but I want to check if it exists.

When I use a simple ‘if exists file path-to-name’ I get a -1409 error.

Can anyone advise a fix please?

Regards

Santa



on SetThePlistPath()
	set thePListFolderPath to path to preferences folder from user domain as string
	set thePListPath to thePListFolderPath & "com.MeSelf.rtfdLoader.plist"
end SetThePlistPath

on EstablishPreferences()
	try
		my SetThePlistPath()

                set theZip to (integer value of button "copyziptodesk" of window 1)
		set theSound to (integer value of button "soundon" of window 1)
		set expandZip to (integer value of button "expandzip" of window 1)
		set emailFlag to (integer value of button "CopyEmail" of window 1)
		set theFiledYear to (title of current menu item of popup button "Year Selector" of window 1)
		set theFiledPlace to (title of current menu item of popup button "Search" of window 1)
		say "this"
		tell application "Finder"
			if exists file thePListPath then
				beep 4
			end if
			if not (exists file thePListPath) then
				
				set theEmptyPListData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict/>
</plist>"
				set thePListFile to open for access thePListPath with write permission
				set eof of thePListFile to 0
				write theEmptyPListData to thePListFile starting at eof
				close access thePListFile
			end if
			
			--set thePListPath to POSIX path of (thePListFolderPath & "com.MeSelf.rtfdLoader.plist")
			tell application "System Events"
				tell property list file thePListPath
					tell contents
						say "trying"
						set value to {|TheZip|:theZip, |TheSound|:theSound, |ExpandZip|:expandZip, |emailFlag|:emailFlag, |TheYear|:theFiledYear, |ThePlace|:theFiledPlace}
						say "success"
					end tell
				end tell
			end tell
		end tell
		
		
	on error errmsg number errnum
		display dialog errmsg & return & return & errnum
	end try
end EstablishPreferences


Hi Santa,

The EstablishPreferences() handler can not see the variable thePListPath
which has been defined in the SetThePlistPath() handler.

In Xcode (btw: you should post Xcode topics in the Xcode forum ;)) it’s strongly recommended to avoid nested application tell blocks
as well as using AppleScript Studio code in application tell blocks, which can cause unexpected behavior

here’s your script with a handler to create the plist file


on createPListFile(PListPath)
	set theEmptyPListData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict/>
</plist>"
	set thePListFile to open for access file PListPath with write permission
	set eof of thePListFile to 0
	write theEmptyPListData to thePListFile starting at eof
	close access thePListFile
end createPListFile

on EstablishPreferences()
	try
		set thePListPath to ((path to preferences folder as text) & "com.MeSelf.rtfdLoader.plist")
		set theZip to (integer value of button "copyziptodesk" of window 1)
		set theSound to (integer value of button "soundon" of window 1)
		set expandZip to (integer value of button "expandzip" of window 1)
		set emailFlag to (integer value of button "CopyEmail" of window 1)
		set theFiledYear to (title of current menu item of popup button "Year Selector" of window 1)
		set theFiledPlace to (title of current menu item of popup button "Search" of window 1)
		say "this"
		tell application "Finder" to set fileExists to exists file thePListPath
		if fileExists then
			beep 4
		else
			createPListFile(thePListPath)
		end if
		
		--set thePListPath to POSIX path of (thePListFolderPath & "com.MeSelf.rtfdLoader.plist")
		tell application "System Events"
			tell property list file thePListPath
				tell contents
					say "trying"
					set value to {|TheZip|:theZip, |TheSound|:theSound, |ExpandZip|:expandZip, |emailFlag|:emailFlag, |TheYear|:theFiledYear, |ThePlace|:theFiledPlace}
					say "success"
				end tell
			end tell
		end tell
		
	on error errmsg number errnum
		display dialog errmsg & return & return & errnum
	end try
end EstablishPreferences

G’day Stefan.

Thank you.

Regards

Santa