Thanks to Yvan Koenig and KniazidisR for helping with duplicating files http://macscripter.net/viewtopic.php?pid=47049#p47049. As they both mentioned in that post the code would not work if run more than once. I actually use that code to duplicate two files, and my main code is designed to delete both duplicated files once the main code is completed. The trick here is my main code is designed to be able to be run multiple times and it renames these duplicated files by appending an incremented numerical value to the end of the filename. So the first time the main code is run it appends a “1” to the end of the filenames, the second time the main code is run it appends a “2” to the end of the filenames, etc.
My main code is made up of three applets; MainOpenEndive.app, OpenEndive_0#.app, and DeleteOpenEndive_0#.app.
MainOpenEndive.app duplicates and renames a 4D Client app and a “template” applet for OpenEndive_0#.app. Then it renames and incremently numbers those apps EndiveName1.app and OpenEndive_01.app (the first time main applet is executed). It then launches OpenEndive_01.app.
OpenEndive_01.app executes the main code for running the 4d Client “EndiveName1.app”. At the end of the applet it deletes the EndiveName1.app once it is no longer running. Then it calls the third applet “DeleteOpenEndive_0#.app”
DeleteOpenEndive_0#.app only function is to delete the OpenEndive_01.app.
My problem is I need to pass to the third applet each time it is executed the correct numerical value for the applet to be deleted. The EndiveName# apps will be quit by the user out of sequence. i.e., app1, app2, app3 are all running, but the user quits app2 first. Now the DeleteOpenEndive_0# applet needs to now which file to delete.
Here’s the code for each of the three applets. In the second applet OpenEndive_0#.app, deleting EndiveName#.app works fine. So I’m trying to declare and set the variable “newFileNameB” to align with the numerical value of EndiveName#.app that it is trying to pass the variable.
How do I pass the variable “newFileNameB” to the third applet? Or does anyone have a better suggestion of how to accomplish this?
Main_OpenEndive (1st Applet to execute)
--duplicate template OpenClient files
set testPath to POSIX file "Applications/Endive Clients/zOpen Clients/GenericEndive.app"
tell application "Finder"
set fileref to duplicate (testPath as alias)
log fileref as string
end tell
set testPath to POSIX file "Applications/Endive Clients/zOpen Clients/OpenEndive_Multi.app"
tell application "Finder"
set fileref to duplicate (testPath as alias)
log fileref as string
end tell
--Rename duplicated template OpenClient files
tell application "Finder"
activate
set name of application file "GenericEndive copy.app" of folder "zOpen Clients" of folder "Endive Clients" of folder "Applications" of startup disk to "EndiveName.app"
set name of application file "OpenEndive_Multi copy.app" of folder "zOpen Clients" of folder "Endive Clients" of folder "Applications" of startup disk to "OpenEndive_0.app"
end tell
--Set OpenEndive_0 to next value
property baseFileNameA : "OpenEndive_0"
set targetDir to POSIX file "/Applications/Endive Clients/zOpen Clients/"
tell application "Finder"
set fileCountA to count of (files in folder targetDir whose name contains baseFileNameA)
end tell
set newFileNameA to "OpenEndive_0" & fileCountA & ".app"
tell application "System Events"
set name of file "/Applications/Endive Clients/zOpen Clients/OpenEndive_0.app" to "/Applications/Endive Clients/zOpen Clients/" & newFileNameA
end tell
--Set EndiveName to next value
property baseFileNameB : "EndiveName"
set targetDir to POSIX file "/Applications/Endive Clients/zOpen Clients/"
tell application "Finder"
set fileCountB to count of (files in folder targetDir whose name contains baseFileNameB)
end tell
set newFileNameB to "EndiveName" & fileCountB & ".app"
tell application "System Events"
set name of file "/Applications/Endive Clients/zOpen Clients/EndiveName.app" to "/Applications/Endive Clients/zOpen Clients/" & newFileNameB
end tell
--Launch OpenEndive_0#
tell application "Finder"
activate
open application file newFileNameA of folder "zOpen Clients" of folder "Endive Clients" of folder "Applications" of startup disk
end tell
OpenEndive_0# (2nd Applet to execute)
--Check for number of existing file
global newFileNameB --attempting to declare global variable "newFileNameB" for use by the 3rd Applet
property baseFileNameA : "EndiveName"
set targetDir to POSIX file "/Applications/Endive Clients/zOpen Clients/"
tell application "Finder"
set fileCountA to count of (files in folder targetDir whose name contains baseFileNameA)
end tell
set newFileNameA to "EndiveName" & fileCountA & ".app"
set appnA to newFileNameA
--Open EndiveName#
tell application "Finder"
activate
open application file newFileNameA of folder "zOpen Clients" of folder "Endive Clients" of folder "Applications" of startup disk
end tell
--Check if EndiveName# is still running
repeat
if application appnA is not running then exit repeat
delay 5
end repeat
--Delete EndiveName# after quiting Endive
set myFile to "/Applications/Endive Clients/zOpen Clients/" & newFileNameA
try
do shell script "rm -rf " & quoted form of myFile
end try
--Launch DeleteOpenEndive_0#
tell application "Finder"
set newFileNameB to "OpenEndive_0" & fileCountA & ".app" --attempting to set the value of global variable "newFileNameB"
activate
open application file "DeleteOpenEndive_0#.app" of folder "zOpen Clients" of folder "Endive Clients" of folder "Applications" of startup disk
end tell
DeleteOpenEndive_0# (3rd Applet to execute) Need to pass the value of variable “newFileNameB” from 2nd Applet
--set newFileNameB to "OpenEndive_0#.app"
set myFile to "/Applications/Endive Clients/zOpen Clients/" & newFileNameB
try
do shell script "rm -rf " & quoted form of myFile
end try