Help with my Application installer

I have developed a simple app called ‘New Project’ which simply duplicates a set of files to a defined location of choice. My trouble is that I want this app to be available to a load of my colleagues and now and then be updated by a simple installer. I have written this piece of code (for the installer), drawing influences from multiple sources and just want to get it working properly. Any help on this would be much appreciated.
BTW, I am very new to developing applescript so forgive me if my code seems really amateur.


property _CDDApps : "CDD Apps"
set _AppsLocation to path to applications folder
set _theApp to ((path to me as string) & "Contents:New Project.app:") as alias

tell application "Finder"
	
	display dialog "You are trying to install a CDD Application called 'New Project'." with icon caution buttons {"Cancel", "Yes! I do know"} default button 2
	
	if the button returned of the result is "Cancel" then
		quit
	else
		
		if not (exists folder _CDDApps of _AppsLocation) then
			make new folder at _AppsLocation with properties {name:_CDDApps}
		end if
		
		set _app to ((path to applications folder as string) & "CDD Apps:") as alias
		
		if not (exists folder "New Project.app" of _app) then
			duplicate _theApp to _app
		else
			display dialog "You have a previous version installed. Would you like to overwite it?" with icon note buttons {"Stop", "Overwrite"} default button 1
			if button returned of result is "Overwrite" then
				try
					delete folder "New Project.app" of _app
				end try
				delay 1
				duplicate _theApp to the _app
				
			else if button returned of result is "Stop" then
				quit
			end if
		end if
		
		display dialog "The installation was successful" buttons {"Cool Beans"} default button 1
		quit
	end if
end tell

I get the error ‘error “Finder got an error: The operation can’t be completed.” number -1426’

If there are easier or better ways on doing this, please advise.

I would also like to make this application ‘New Project’ available in the Finder Toolbar. Any ideas as to how I can do this through this installer?

…and thanks in advance.

Model: iMac
AppleScript: 2.2.1
Browser: Safari 537.22
Operating System: Mac OS X (10.8)

Hello.

I think the problem might be that you are copying to the /Applications folder, and if your users doesn’t have administrative rights, then this operation will fail.

One way to fix this is to use the install utlity (see man install) and a do shell script with adminstrator privileges to ensure that the user has enough rights to perform the operation, if that is the fault.

A second scheme, is to use the applications folder from user domain aka ~/Applications - the users private applications folder. You’ll have to check if it exists, because it has to made if it it isn’t there, as this folder isn’t made automatically.

HTH. and Welcome to Macscripters. :slight_smile:

Hello. Since this is a topic I am quite taken of at the moment, I made it work, according to my hypothesis above.
But I haven’t cleaned it up in any way.

It now falls back to even creating the users local applications folder if it hasn’t the rights to create a folder in the /Applications folder.

If something isn’t working, have a look in the CDD log in the console.app.


property _CDDApps : "CDD Apps"
on run
	set _AppsLocation to path to applications folder
	set _AppsFallback to path to applications folder from user domain
	set _theApp to ((path to me as string) & "Contents:New Project.app:") as alias
	
	tell application "Finder"
		
		display dialog "You are trying to install a CDD Application called 'New Project'." with icon caution buttons {"Cancel", "Yes! I do know"} default button 2
		
		if the button returned of the result is "Cancel" then
			tell me to quit
		else
			try
				if not (exists folder _CDDApps of _AppsLocation) then
					my logit("CDD: folder didn't exist", "CDD")
					make new folder at _AppsLocation with properties {name:_CDDApps}
				end if
				set fallback to false
			on error e number n
				my logit("CDD:" & e & ":" & n & "failed creating folder", "CDD")
				if n = -10000 then -- Fallback to users applications folder 
					set fallback to true
				end if
			end try
			if not fallback then
				set _app to ((path to applications folder as string) & "CDD Apps:") as alias
			else
				set failed to false
				try
					if not (exists folder _AppsFallback) then
						make new folder at home with properties {name:"Applications"}
					end if
				on error e number n
					my logit("CDD:" & e & ":" & n & "failed creating users application folder", "CDD")
					set failed to true
				end try
				if not failed then
					try
						make new folder at _AppsFallback with properties {name:_CDDApps}
						set _app to ((path to applications folder from user domain as string) & "CDD Apps:") as alias
					on error e number n
						my logit("CDD:" & e & ":" & n & "failed creating folder at fallback", "CDD")
						set failed to true
					end try
				end if
				if failed then
					tell me
						activate
						display alert "Something went wrong when making ~/Applications/CDD Apps!
Quitting for now."
						quit
					end tell
				end if
			end if
			if not (exists folder "New Project.app" of _app) then
				duplicate _theApp to _app
			else
				display dialog "You have a previous version installed. Would you like to overwite it?" with icon note buttons {"Stop", "Overwrite"} default button 1
				if button returned of result is "Overwrite" then
					try
						delete folder "New Project.app" of _app
					end try
					delay 1
					duplicate _theApp to the _app
					
				else if button returned of result is "Stop" then
					tell me to quit
				end if
			end if
			
			display dialog "The installation was successful" buttons {"Cool Beans"} default button 1
			tell me to quit
		end if
	end tell
end run

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

Many thanks McUsrII. I’m really grateful for your help.

I have updated the script and developed it to include a repeat function that will go through multiple apps.
I’m finding that it’s not running the function to duplicate each application separately. I also wanted to ask whether it’s possible to add each application to the finder toolbar. Do you know if that is possible?


property _CDDApps : "CDD Apps"
on run
	set _AppsLocation to path to applications folder
	set _AppsFallback to path to applications folder from user domain
	set folderToProcess to ((path to me as string) & "Contents:")
	
	tell application "Finder"
		
		display dialog "You are installing CDD Applications." with icon caution buttons {"Cancel", "Yes! I do know"} default button 2
		
		if the button returned of the result is "Cancel" then
			tell me to quit
		else
			try
				if not (exists folder _CDDApps of _AppsLocation) then
					my logit("CDD: folder didn't exist", "CDD")
					make new folder at _AppsLocation with properties {name:_CDDApps}
				end if
				set fallback to false
			on error e number n
				my logit("CDD:" & e & ":" & n & "failed creating folder", "CDD")
				if n = -10000 then -- Fallback to users applications folder 
					set fallback to true
				end if
			end try
			if not fallback then
				set _app to ((path to applications folder as string) & "CDD Apps:") as alias
			else
				set failed to false
				try
					if not (exists folder _AppsFallback) then
						make new folder at home with properties {name:"Applications"}
					end if
				on error e number n
					my logit("CDD:" & e & ":" & n & "failed creating users application folder", "CDD")
					set failed to true
				end try
				if not failed then
					try
						make new folder at _AppsFallback with properties {name:_CDDApps}
						set _app to ((path to applications folder from user domain as string) & "CDD Apps:") as alias
					on error e number n
						my logit("CDD:" & e & ":" & n & "failed creating folder at fallback", "CDD")
						set failed to true
					end try
				end if
				if failed then
					tell me
						activate
						display alert "Something went wrong when making ~/Applications/CDD Apps!
Quitting for now."
						quit
					end tell
				end if
			end if
			
			set fileExt to {".app"}
			set theTopFolder to (folderToProcess as alias)
			repeat with EachFile in (get every file of folder (folderToProcess as alias))
				try
					copy name of EachFile as string to FileName
					repeat with ext in fileExt
						if FileName ends with ext then
							
							if not (exists folder EachFile of _app) then
								duplicate EachFile to _app
							else
								display dialog "You have a previous version installed. Would you like to overwite it?" with icon note buttons {"Stop", "Overwrite"} default button 1
								if button returned of result is "Overwrite" then
									try
										delete folder "New Project.app" of _app
									end try
									delay 1
									duplicate EachFile to the _app
									
								else if button returned of result is "Stop" then
									tell me to quit
								end if
							end if
							
						end if
					end repeat
				end try
			end repeat
			
			display dialog "The installation was successful." buttons {"Cool Beans"} default button 1
			tell me to quit
		end if
	end tell
end run

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

Hello.

Well done! :slight_smile:

I suggest you take the last piece of code, and plugs in two different set of directories, from and to, and then you watch what is going on in the log window of Applescript Editor. Be liberal with log statements!

I also suggest that you use version numbering. yourapp-02.app should then contain whatever-02.app.

By the way:

That inner repeat loop of yours with file.ext, I don’t know what it is supposed to do, as you repeat over every file, it seems also to me, that this usage of fileext, short circuits your code, from reading it, since name does not necessarily contain the name extension

Maybe it is better to get your files like this:


	set f to every file of folder ((path to me as string) & "Contents") whose name extension is "app"

And then perform your tests, dropping the repeat loop?