Launching an app from within bundle/add + replace aliases

This is a two part question thread. I’m still in the ‘beginner’ grade of scripting. :smiley:

The scenario: A p2p file-sharing application which will delete its connection file if the modification date is older than 30 days. I need to try to prevent this happening. Connection files should be good for at least 3 months.

I thought ideally a script running from within the application bundle would be ideal if it could ‘somehow’ be triggered to run as the main application is opening and change the connection file’s modification date. However I do not have a clue how to do this to the application bundle. And I suspect bundle info files are probably highly sensitive.

Second best solution I could put together: The workaround is a script that changes the connection file’s modification date, then launches the application.

If anybody has any ideas about the ‘ideal’ solution I would love to hear them.

In meantime, this is what I put together:

code checked out

Is there anything wrong with the design/layout of the script? It runs fine on my system.

If I cannot do the ‘ideal’ solution, then this script app will suffice but I would like some advice on creating an installer for this script app. if possible.

For the actual installer of the script app, this is what I put together:
(This is a post-install section of the script-app installer dealing with aliases of the script-app. This is where my questions lie.)

(Adding to Dock looks complicated. ‘I would have liked to replace the original application’s aliases with the script app’s aliases.’ But that looks like a major scripting job for advanced scriptors lol)

References: Create alias to desktop and Add items to Dock.

I have not tried this out yet other than it compiling ok. I’m a little nervous lol. Does this look right?

set the_choice to (choose from list {"Add to Dock", "Add alias to Desktop", "None"} with prompt "Add aliases for appname?:") as string
if the_choice is "false" then error number -128
try
	if word 1 of the_choice is "Add" then
		if word 3 of the_choice is "Dock" then
			set the_file to "appname"
			set persistent_what to "persistent-apps"	
			
			set this_file to POSIX path of (item i of the_file as alias)
			do shell script "defaults write com.apple.dock " & persistent_what & " -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & this_file & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
			
			tell application "Dock"
				quit
				repeat
					try
						activate
						exit repeat
					end try
				end repeat
			end tell
			
		else if word 2 of the_choice is "alias" then
			set apps_path to (path to At Ease applications folder from local domain) as string
			try
				set app_ref to (apps_path & "appname.app") as alias
			on error -- app is not in Applications folder
				set app_ref to (choose file with prompt "Where is the appname application?")
			end try
			
			tell application "Finder"
				make alias to app_ref at desktop
			end tell
			
		else
			if word 1 of the_choice is "None" then
				close
			end if
		end if
	end if
end try

I guess I could also put in an option for aliases to both desktop and dock choices.

Don’t tinker with application bundles. Your ‘2nd best’ solution is the best solution: a proxy script that you run instead of the app.

Your second script is needlessly complicated. This is a skeleton:

set the_choice to (choose from list {"Add to Dock", "Add alias to Desktop", "None"} with prompt "Add aliases for LWStarter?:" without multiple selections allowed) as text

-- script always needs to know location of app "LWstarter"
-- so put that here, or ask for it here

if the_choice is "Add to Dock" then
	-- add to dock
else if the_choice is "Add alias to Desktop" then
	-- alias on desktop
end if
-- no need to do anything with the third choice
-- the script will just end

This cannot work:

	set the_file to "LWStarter"
	set this_file to POSIX path of (item i of the_file as alias)

If this bit is good syntax it will REPLACE all apps in the dock with the one you select. Is that what you want?

do shell script "defaults write com.apple.dock " & persistent_what & " -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & this_file & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"

I strongly suggest you work out the various bits on their own, before putting them in the script.
I find that making a skeleton, with comments in place of the working bits, is helpful to prevent spaghetti code.
Think first, code later :wink:

Thanks for your reply and pointers. I had it so wrong.
This seems to work, but I presume it is universal OSX compatible for the dock?

set the_choice to (choose from list {"Add to Dock", "Add alias to Desktop", "None"} with prompt "Add aliases for appname?:") as string
if the_choice is "false" then error number -128
try
	if the_choice is "Add to Dock" then
		
		do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/appname.app/</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"  -- adds app to dock
		
		tell application "Dock"
			quit
			repeat
				try
					activate   -- necessary forced restart of dock for new listing to show. 
   -- Otherwise different results for different OSX's from what I have read. (Otherwise Dock might not restart)
					exit repeat
				end try
			end repeat
		end tell
	else if the_choice is "Add alias to Desktop" then
		set apps_path to (path to At Ease applications folder from local domain) as string
		try
			set app_ref to (apps_path & "appname.app") as alias  -- creates desktop alias
		on error -- app is not in Applications folder
			set app_ref to (choose file with prompt "Where is appname.app?")
		end try
		
		tell application "Finder"
			make alias to app_ref at desktop  -- creates desktop alias if could not be found in application folder.
		end tell
	end if
end try

Should I be doing anything upon error number -128?

I feel shy to ask questions sometimes in case the experts just think it is too simplistic or dumb.

Is the structure of this ok? I have removed the middle part of the code (in previous post):

display alert "Choose Language" message "Select installer for english or francais" as critical buttons {"english", "Francais"} default button "english" --- inital installer menu to choose language text for installer
set response to button returned of the result

if response is "english" then
	display dialog ("english text ... 
Click OK to install app") --- english installer menu
	
else if response is "francais" then
	display dialog ("french text ...") --- french installer menu
end if

set thisSource to path to resource "application name" -- location of app to install within package
set apps_path to (path to At Ease applications folder from local domain) as string -- destination to applications folder
tell application "Finder" to duplicate thisSource to apps_path -- technique for the installing

.. (code from previous post goes here)

--- last section is not really necessary, but just a finale and announcement that all is finished with option to go to website should there be any problems.
if response is "english" then
	display alert "FINISHED." message "If any questions or problems, go to the ... Forums for assistance" as critical buttons {"Quit", "Go to website"} default button "Quit"
	... "http://www.."
	
else if response is "francais" then
	display alert "Mà J exécutée" message "french equivalent text" as critical buttons {"Quitter", "Allez sur le site"} default button "Quitter"
	.... "http...francais/"
	
end if

quit

This all seems to work. However, I do not know whether it is due to the dock being restarted several times in testing, but sometimes the script.app is installed with a white circle and white line through it, also in the dock (but still works.) This only seems to occur in List View. If folder view is set to Icons, there never seemed to be any issue. That’s not really an applescript issue I guess but just hoping that does not happen to others who use this installer. The app has its own custom icon. I guess I would be more worried if the crossed-circle on the icon was red. From what I googled, it might just be a finder.plist issue, and lack of updating properly.

The script ignores everything except “Add to Dock” and “Add alias”. You do not need that line.

In the ‘Add to dock’ section you’re using a hard-coded location for your app.
Yet in the ‘Add alias’ section you are providing for the absence of the app. What’s the logic?
This stuff should be placed earlier in the script, as I showed in my skeleton code. In that way you’ll have to change things just once, when the time comes.

“At Ease” is from the Stone Age - remove that. It’ll break your script without warning. It’s ignored in Snow Leopard. Mountain Lion may choke on it.

I have no comments on your installer. if what we worked out last time ran OK you only need to change the payload name and its destination (wise to use variables for them, and put their values at the top of the script, so it’s quickly modified for new uses).

Thank you for your advice and pointers again. :slight_smile: I have changed to ‘applications’ instead. Removed the unnecessary lines.

Moved the set thisSource and set apps_path to top of script.

If I were doing this for more than 2 languages then I would have looked at support language files to load and replace variables for the menus. This is something I have not seriously tried doing before, well I did try last year but not with success. That would be another thing to learn how to do in future.