Script works from desktop but not Applications folder

Hi Everyone, I have this script working fine from my desktop, but when I try to address to my applications folder, it keeps writing the plist as if it doesn’t “exist”. Here are both scripts. I know it’s probably simple, but I have tried literally dozens of addressing, and I’m at a loss. I am also getting an error in the latter part of the script.Thanks for your help.

–This one works

set prefsFolder to (path to desktop folder from user domain) as text
set plistFile to (prefsFolder & "IP_Notifier.plist")

tell application "Finder"
	if (exists file plistFile) then
		set theFolder to (path to desktop folder from user domain) as text
		set ipScript to POSIX path of (theFolder & "NewIPAddress.scpt")
		run script (ipScript)
	else
		set plistfile_path to POSIX path of (prefsFolder & "IP_Notifier.plist")
		set the_record to {now:current date, emailaddress:"text", username:"text", ipaddress:"text", testbool:"false", testbool1:"false", testbool2:"false"}
		tell application "System Events"
			set the parent_dictionary to make new property list item with properties {kind:record, value:the_record}
			set pl to make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
			tell application "Finder"[b]--This one works too[/b]
				set theFolder to (path to desktop folder from user domain) as text
				set saveToPlist to POSIX path of (theFolder & "writetoplist.scpt")
				run script (saveToPlist)
			end tell
		end tell
	end if
end tell

This one doesn’t work

set prefsFolder to (path to applications folder from system domain) as text
set plistFile to "New IP Address.app/Contents/IP_Notifier.plist"
set prefFilePath to prefsFolder & plistFile

tell application "Finder"
	if (exists file prefFilePath) then --Doesn't see it, moves to"else" and writes over the existing plist
		set theFolder to "/Applications/New IP Address.app/Contents/Resources/"
		set ipScript to POSIX path of (theFolder & "NewIPAddress.scpt")
		run script (ipScript)
	else
		set plistfile_path to (path to applications folder from system domain) as text
		set plistFile to plistfile_path & "New IP Address.app/Contents/IP_Notifier.plist"
		set the_record to {now:current date, emailaddress:"text", username:"text", ipaddress:"text"}
		tell application "System Events"
			set the parent_dictionary to make new property list item with properties {kind:record, value:the_record}
			set pl to make new property list file with properties {contents:parent_dictionary, name:plistFile}
			tell application "Finder"
				set theFolder to (path to applications folder from system domain) as text
				set saveToPlist to POSIX path of (theFolder & "/New IP Address.app/Contents/Resources/writetoplist.scpt")
				run script (saveToPlist) --"Finder got an error: A "/" can't go here."
			end tell
		end tell
	end if
end tell
/applescript]

What you get is perfectly logical.


set prefsFolder to (path to applications folder from system domain) as text
set plistFile to "New IP Address.app/Contents/IP_Notifier.plist"
set prefFilePath to prefsFolder & plistFile

tell application "Finder"
	prefFilePath
	--> "Macintosh HD:Applications:New IP Address.app/Contents/IP_Notifier.plist"
end tell

Finder is unable to work with a mix of HFS and Unix path.

Would be more efficient to take care of that and code :


set prefsFolder to (path to applications folder from system domain) as text
set plistFile to "New IP Address.app:Contents:IP_Notifier.plist"
set prefFilePath to prefsFolder & plistFile

tell application "Finder"
	prefFilePath
	--> "Macintosh HD:Applications:New IP Address.app:Contents:IP_Notifier.plist"
end tell

I wish to add that the instruction :

set prefsFolder to (path to applications folder from system domain) as text
is a bit surprizing.
It would be more logical to use :
set prefsFolder to (path to applications folder from local domain) as text
because the Applications folder isn’t in the System folder.

Yvan KOENIG (VALLAURIS, France) vendredi 6 avril 2012 17:03:45

some notes

the parameter from of path to is only needed to refer to something else as the default location

path to desktop - default location is user domain
path to applications folder - default location is local domain

consider that there is also an as parameter

(path to desktop) as text

is a coercion alias to path string

(path to desktop as text)

gets the path string directly by the as parameter

The most portable way is to use path to me which refers to the script/application itself.
The recommended location for plist files is the Resources folder, for scripts the folder Resources:Scripts:. The Contents folder of a package is supposed to be empty.

As Yvan explained, your script works fine using exclusively HFS paths


set NewIPAddressAppResourcesFolder to (path to me as text) & "Contents:Resources:"
set NewIPAddressAppScriptsFolder to NewIPAddressAppResourcesFolder & "Scripts:"
set prefFilePath to NewIPAddressAppResourcesFolder & "IP_Notifier.plist"

tell application "Finder" to set prefFileExists to exists file prefFilePath
if prefFileExists then --Doesn't see it, moves to"else" and writes over the existing plist
	set ipScript to alias (NewIPAddressAppScriptsFolder & "NewIPAddress.scpt")
	run script ipScript
else
	set the_record to {now:current date, emailaddress:"text", username:"text", ipaddress:"text"}
	tell application "System Events"
		set the parent_dictionary to make new property list item with properties {kind:record, value:the_record}
		set pl to make new property list file with properties {contents:parent_dictionary, name:prefFilePath}
	end tell
	set saveToPlist to alias (NewIPAddressAppScriptsFolder & "writetoplist.scpt")
	run script saveToPlist
end if

Thank you for the replies Yvan and Stephan. I was messing around with Yvan’s suggestion, after fixing the mixed HFS and Unix paths, I kept getting "cant have an identifier after an identifier on this part, “run script (ipScript)”

Stephan, That’s what I was looking for, as I am hoping to have this run on anyones machine with any user logged on. I am having a problem with this one also. I saved my script as an app, and it changed the script name to “main script”. That’s OK, but when using the “path to me” it adds its complete path, not just the path to its folder. I need something like “path to my Resources folder”, or “path to me without :scripts:main.scpt”, Am I doing something wrong?

“File alias Macintosh HD:Applications:IP Notifier.app:Contents:Resources:Scripts:main.scpt:Scripts:NewIPAddress.scpt of «script» wasn’t found.”

Thanks so much for your help. I spend many days searching and trying things before asking for help.

path to me points always to the .app bundle or in case of a non-bundle script to the script

set NewIPAddressAppResourcesFolder to (path to me as text) & "Contents:Resources:"

points to the resources folder of the application bundle

Stephan, you are right. I was trying to run the script from script editor, as in testing. Thats what was messing me up. I just accidentally double clicked the icon(instead of show package contents) and it ran fine. Thanks for your help.

I have one more question. This works if I change the app name to IP,Notifier.app., but I want to get rid of the comma. I would like the app name to be IP Notifier.app, not IP,Notifier.app, but I cant leave the space in the folder path. Thanks again, I learned a ton today. The POSIX path clicked today

set prefsFolder to (path to applications folder) as text
set prefsFolderpath to “IP,Notifier.app:Contents:Resources:”
set plistFile to POSIX path of (prefsFolder & prefsFolderpath & “IP_Notifier”)

set username to (do shell script “defaults read " & plistFile & " username”)

in the shell you must quote paths containing space characters, because spaces are parameter separators

set prefsFolder to path to applications folder as text
set prefsFolderpath to "IP Notifier.app:Contents:Resources:"
set plistFile to POSIX path of prefsFolder & prefsFolderpath & "IP_Notifier"

set username to (do shell script "defaults read " & quoted form of plistFile & " username")

Hi StefanK, I’m sorry, I just noticed I’ve been misspelling your name. I tried it with just the space and couldn’t get it to work. Then I changed the app name to IP,Notifier(with a comma) to see if it would work. When I try it with just a space, applescript is seeing a comma, and a space. Here is the error.
Thanks again

error “2012-04-06 18:38:22.692 defaults[11460:60f]
The domain/default pair of (/Applications/IP, Notifier.app/Contents/Resources/IP_Notifier) does not exist”

sorry, of course the part prefsFolderpath must be coerced to POSIX path


set plistFile to POSIX path of (prefsFolder & prefsFolderpath) & "IP_Notifier"