Running Multiple Scripts, One After the Other

I have three scripts that I would like to run on a regular basis. Right I’m using Automator to run these one after the other. Is there a way to have one script, when it closes, to trigger the second script, and that trigger the third script when it’s done?

Generally speaking, you just need to call them in order. However, it’s a good idea to keep the three scripts together in isolation so that they don’t affect each other.

run of scriptObj1
run of scriptObj2
run of scriptObj3


script scriptObj1
	on run
		display dialog "One"
	end run
end script

script scriptObj2
	on run
		display dialog "Two"
	end run
end script

script scriptObj3
	on run
		display dialog "Three"
	end run
end script

Thank you. But, me being very new to AppleScript I have two questions.
First, do I need to rename my scripts to script0bj1, script0bj2 and script0bj3?
Second, if I don’t want anything displayed, want do I delete?

There is no problem if you can put three scripts in one.

However, it will be difficult to find a collision when using the same variable, the same property, the same handler name, etc.

Therefore, we provided proposals to integrate these three scripts while dividing the logical category.

The display dialog command is not required because it is displayed only as a simple description.

Homer712. I’ve never had occasion to do what you want, but the AppleScript Language Guide recommends the following approach. Each of the following is a separate script file, and they worked as expected on my computer. You will have to change the script paths to the those applicable on your computer.

-- each of the following are separate script files

-- script file 1
say "Good Morning"
set script2 to "Macintosh HD:Users:Robert:Working:Test 2.scpt" as alias
run script script2

-- script file 2
say "Good Afternoon"
set script3 to "Macintosh HD:Users:Robert:Working:Test 3.scpt" as alias
run script script3

-- script file 3
say "Good Evening"

BTW, depending on what each script does, there could be timing issues, and it’s good to be aware of that. Also, the AppleScript Language Guide discussion of the run script command can be found here

Thanks, but now that I’ve installed Script Debugger, and saving a script as an application works each and every time . . . perfectly, the reason for the question is moot. I posted when I was having issues on the 2020 M1 MacBook Pro combining several scripts into one and having it continually fail no matter how many times I added/deleted/added back again it the the Security & Privacy/Accessibility listing.

But, I have copied your script and will set it up just for the learning process, so thanks again.

Ran perfectly through all three scripts. And here’s what I’m learning. There’s either some software issue with my MacBook Pro, or Apple does not have its act together, and based on the last few macOS system releases, I would bet on the latter. When I saved the first script as an application from within Script Editor, it would run but I would get multiple popup windows saying that Test Script 1 would like to have access to my desktop. Each and every time I ran it. Opened the script in Script Debugger, saved it as an application, and done. Asked me just once if Test Script 1 could have access to my desktop and from there on it ran flawlessly every time I ran it. So, the learning continues.

After playing around with the script you posted, I was able to get three scripts to run perfectly one after the other, after the other. My script is quite simple:

run script "Path:To:Script:Script Name.scpt" as alias
run script "Path:To:Script:Script Name.scpt" as alias
run script "Path:To:Script:Script Name.scpt" as alias

Question, is the “as alias” needed?

Why don’t you ry it and find out!
Comment out the “as alias” part

Thanks, you’re right, if I’m going to learn, I should have just tried it. The result was an error. I think I’ll do some reading on the whole “alias” thing.
Error

That’s a weird error, as there are no colons in the code.
What did you use to comment out the code? In AppleScript it is 2 dashes “–”

run script "Path:To:Script:Script Name.scpt" --as alias

Here’s the script as “commented out.”

run script "Users:homer:Documents:MacBook Pro Specific:AppleScript Files:Mac Specific Listing.scpt" --as alias
run script "Users:homer:Documents:MacBook Pro Specific:AppleScript Files:XMenu Listing.scpt" --as alias

And the error:
Error

The scripting dictionary for Standard Additions says that the run script command requires “the script text (or an alias or file reference to a script file)”.

You’re passing it a text string: the error message is less than helpful.

ADD: this is because your script with “as alias” commented out understands the string to be a script, and is trying to compile and run this:

Users:homer:Documents:MacBook Pro Specific:AppleScript Files:Mac Specific Listing.scpt

You will get the same error if you try to compile and run that in a script editor.

Thanks, makes sense, sort of . . .

I’m just starting out, but I googled “scripting dictionary for Standard Additions” in your post and was able to download a pdf of Apple’s “AppleScript Scripting Additions Guide.” Bit beyond my understanding right now, but it’ll make for a good reference once I have more experience with scripts.

Homer712. As you’ve discovered, the run script command works with an alias object, but it will also work with a file object, as in the following:

run script file "Macintosh HD:Users:Robert:Working:Test 1.scpt"

The AppleScript Language Guide describes the difference between alias and file objects as follows:

An alias object is a dynamic reference to an existing file system object. Because it is dynamic, it can maintain the link to its designated file system object even if that object is moved or renamed.

A file object represents a specific file at a specific location in the file system. It can refer to an item that does not currently exist, such as the name and location for a file that is to be created. A file object is not dynamic, and always refers to the same location, even if a different item is moved into that place.

With recent versions of macOS, aliases do not always work as originally intended, and, in those instances, it’s probably best to use a file object. The following are some examples of how alias and file objects are created and used:

set theFile to "Macintosh HD:Users:Robert:Desktop:Test.txt" -- an HFS path
set theFile to "/Users/Robert/Desktop/Test.txt" -- a POSIX path

set theFile to "Macintosh HD:Users:Robert:Desktop:Test.txt" as alias -- create an alias object
set theFile to "Macintosh HD:Users:Robert:Desktop:Test.txt" as «class furl» -- create a file object
set theFile to POSIX file "/Users/Robert/Desktop/Test.txt" -- create a file object

tell application "Finder" to open alias "Macintosh HD:Users:Robert:Desktop:Test.txt" -- works OK
tell application "Finder" to open file "Macintosh HD:Users:Robert:Desktop:Test.txt" -- works OK
tell application "Finder" to open "Macintosh HD:Users:Robert:Desktop:Test.txt" -- works but probably shouldn't

set theFile to (choose file) -- returns an alias object
set theFile to (choose file name) -- returns a file object

try -- check to see if a file exists
	alias "Macintosh HD:Users:Robert:Working:Test.txt"
on error
	display dialog "The file Test.txt does not exist"
end try

Thank you for always providing an explanation of how things work, and sometimes, why they do not. Much appreciated. I actually copy/paste some of your responses into notes that I keep to be able to refer back to.

Today I attempted to write a script for the following situation. I am much better versed in rsync than I am in AppleScript. I have an rsync routine that not only does backups for me, but also puts all changed/deleted files into dated folders so that I have backups of those changed/deleted files. Over time I’ve ended up with dated folders that go back many months. My thought was that after say 30 days, if I’ve had no need to go back to those archives it would be good to have a script that looked at all those folders and deleted those that are over 30 days old. Took a while, and many tries, but I was finally able to put such a script together. Works perfectly.

tell application "Finder"
	set theFolderAlias to alias "Users:homer:Desktop:Sync Folders Local Backups:Local Documents Archive:"
	delete (get items of theFolderAlias whose creation date < ((current date) - 30 * days))
end tell

tell application "Finder"
	set theFolderAlias to alias "Users:homer:Desktop:Sync Folders Local Backups:Local iCloud Folder Archive:"
	delete (get items of theFolderAlias whose creation date < ((current date) - 30 * days))
end tell

I have many more folders to add, but now it’s just a matter of copying the path to each, and adding them to the script. Thanks for helping me along. It’s been a good day!

According to your script sample, the path is to a sub-item on a disk named “Users”

Here is a version that uses “System Events” instead of “Finder”

tell application "System Events"
	repeat with theFolderAlias in {folder "Mac SSD:Users:robert:Desktop:Sync Folders Local Backups:Local Documents Archive:", folder "Mac SSD:Users:robert:Desktop:Sync Folders Local Backups:Local iCloud Folder Archive:"}
		set theFolderAlias to contents of theFolderAlias
		set myItems to (disk items of theFolderAlias whose creation date < ((current date) - 30 * days))
		repeat with anItem in myItems
			set anItem to contents of anItem
			delete anItem
		end repeat
	end repeat
end tell

Actually, the “real” path is (at least to the first of seven archive folders): /Volumes/MacBook Pro Local Backups/Sync Folders Local Backups/Local Documents Archive

What I did when trying to put the script together was to copy the entire backup folder to my desktop, as I didn’t want to inadvertently have my script delete any valuable files/folders from my actual backup location. And, initially I was using the modification date. Changed that to creation date after the first of my many tries with the first folder worked (all went into the trash, but not emptied) and I did a “Put Back” from the trash. Could not understand why it wouldn’t work a second time until I noticed that once you do a “Put Back” the modification dates change. :thinking:

I’m thinking the reason you posted the System Events script is that there must be an advantage to using it over Finder, correct?

Correct. Too many issues with speed when using “whose” clauses. Also on some of my Macs, I quit the Finder when using other apps.

To a certain extent, the decision to use System Events instead of Finder is a matter of personal preference. However, there are some differences to be aware of:

  • System Events if faster.
  • Finder has an entire-contents property which allows you to get the contents of a folder recursively. System Events has no such property, although it can be written to act recursively.
  • The System Events delete command actually deletes files, while the Finder delete command moves them to the trash.
  • System Events returns hidden files. Finder returns some hidden files but only if the Finder window is set to show hidden files.
  • System Events has a few annoying bugs when manipulating alias files.