Help With Combining Two Scripts

I have a script that does some backup of a few files prior to my doing my backups. I call it my Pre Backup Script. This is it below.

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Thunderbird/Profiles/7jyt1da7.default-release/abook.sqlite" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/Address Book Sync" with replacing

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Thunderbird/Profiles/7jyt1da7.default-release/history.sqlite" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/Address Book Sync" with replacing

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Application Support/Firefox/Profiles/jla842kt.default-release/bookmarks.html" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/Firefox Bookmarks Sync" with replacing

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Application Support/Firefox/Profiles/jla842kt.default-release/favicons.sqlite" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/Firefox Bookmarks Sync" with replacing

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Application Support/Firefox/Profiles/jla842kt.default-release/places.sqlite" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/Firefox Bookmarks Sync" with replacing

tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
	"/Users/homer/Library/Application Support/ChronoSync/Tasks" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/Backup Scripts/ChronoSync Tasks" with replacing

tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
	"/Users/homer/Library/Application Support/XMenu/Custom" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/XMenu Sync" with replacing

tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
	"/Users/homer/Library/Application Support/SpamSieve" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/SpamSieve Backup" with replacing

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Preferences/com.c-command.SpamSieve.plist" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/SpamSieve Backup/Preference File" with replacing

tell application id "com.apple.Finder" to duplicate file POSIX file ¬
	"/Users/homer/Library/Application Scripts/com.apple.mail/Apple Mail - Remote Training.scpt" to the folder ¬
	POSIX file "/Users/homer/Documents/MacBook Pro Specific/SpamSieve Backup/AppleScript" with replacing

display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

The problem with this script (ever since I upgraded to Sonoma from Monterey is that the notification no longer goes away as it did after a couple of seconds in Monterey.

So, I found this script that dismisses notifications, and it works just fine fine, when run on its own. Found it here: AppleScript to close all notifications on macOS Big Sur, Monterey, and Ventura · GitHub, almost all the way down the page in a September 20th post by “bpetrynski”.

-- macOS 15 Sequoia
-- Set of close action phrases in multiple languages
property closeActionSet : {"Close", "Clear All", "Schließen", "Alle entfernen", "Cerrar", "Borrar todo", "关闭", "清除全部", "Fermer", "Tout effacer", "Закрыть", "Очистить все", "إغلاق", "مسح الكل", "Fechar", "Limpar tudo", "閉じる", "すべてクリア", "बंद करें", "सभी हटाएं", "Zamknij", "Wyczyść wszystko"}

-- Function to perform close action on a given element
on closeNotification(elemRef)
	tell application "System Events"
		try
			set theActions to actions of elemRef
			repeat with act in theActions
				if description of act is in closeActionSet then
					perform act
					return true
				end if
			end repeat
		end try
	end tell
	return false
end closeNotification

-- Function to recursively search for and close notifications
on searchAndCloseNotifications(elemRef)
	tell application "System Events"
		-- If the element has subelements, search them first
		try
			set subElements to UI elements of elemRef
			repeat with subElem in subElements
				if my searchAndCloseNotifications(subElem) then
					return true
				end if
			end repeat
		end try
		
		-- Try to close the current element if it's a notification
		if my closeNotification(elemRef) then
			return true
		end if
	end tell
	return false
end searchAndCloseNotifications

-- Main script to clear notifications
on run
	tell application "System Events"
		if not (exists process "NotificationCenter") then
			log "NotificationCenter process not found"
			return
		end if
		
		tell process "NotificationCenter"
			if not (exists window "Notification Center") then
				log "Notification Center window not found"
				return
			end if
			
			set notificationWindow to window "Notification Center"
			
			-- Main loop to clear notifications
			repeat
				try
					if not my searchAndCloseNotifications(notificationWindow) then
						-- If no notifications were closed, we're done
						exit repeat
					end if
					
					-- Reduced delay to speed up the script
					delay 0.1
				on error errMsg
					-- If an error occurs, log it and exit the loop
					log "Error: " & errMsg
					exit repeat
				end try
			end repeat
		end tell
	end tell
end run

To make things simple, I tried just adding this script to my Pre Backup Script, but end up getting an error (see screenshot).

Anyone have any ideas on how to resolve this?

Use “script” to combine multiple script which has same handler or property names.

set aParam to "Piyomaru Software"

set nRes to parseByChar(aParam) of norm
--> {"P", "i", "y", "o", "m", "a", "r", "u", " ", "S", "o", "f", "t", "w", "a", "r", "e"}
set rRes to parseByChar(aParam) of rev
--> {"e", "r", "a", "w", "t", "f", "o", "S", " ", "u", "r", "a", "m", "o", "y", "i", "P"}


script norm
	on parseByChar(aText)
		return characters of aText
	end parseByChar
end script

script rev
	on parseByChar(aText)
		return reverse of characters of aText
	end parseByChar
end script

That error means that you have top level commands in your script (outside any handler, but doesn’t include property declarations) and an run handler:

on run
-- your script
end run

The simplest solution is to remove the On run/end run lines, or move all the top level parts of your script into the run handler.

First I cleaned up your first script by consolidating into 1 tell block

tell application id "com.apple.Finder"
	duplicate POSIX file "/Users/homer/Library/Thunderbird/Profiles/7jyt1da7.default-release/abook.sqlite" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/Address Book Sync" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Thunderbird/Profiles/7jyt1da7.default-release/history.sqlite" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/Address Book Sync" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Support/Firefox/Profiles/jla842kt.default-release/bookmarks.html" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/Firefox Bookmarks Sync" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Support/Firefox/Profiles/jla842kt.default-release/favicons.sqlite" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/Firefox Bookmarks Sync" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Support/Firefox/Profiles/jla842kt.default-release/places.sqlite" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/Firefox Bookmarks Sync" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Support/ChronoSync/Tasks" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/Backup Scripts/ChronoSync Tasks" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Support/XMenu/Custom" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/XMenu Sync" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Support/SpamSieve" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/SpamSieve Backup" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Preferences/com.c-command.SpamSieve.plist" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/SpamSieve Backup/Preference File" with replacing
	
	duplicate POSIX file "/Users/homer/Library/Application Scripts/com.apple.mail/Apple Mail - Remote Training.scpt" ¬
	to the POSIX file "/Users/homer/Documents/MacBook Pro Specific/SpamSieve Backup/AppleScript" with replacing
end tell
display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

The removal of the “on run/end run” lines did the trick, thanks.

After removing the on run/end run lines, I also used your revised/cleaned up script, and everything is now running smoothly, no errors. Thank you too!

I also have the same script running on my 2018 Intel Mac mini, which I will clean up as well. Seeing things like your cleaned up version of my pre backup script helps with the learning process, which is why I joined MacScripter in the first place, so thanks again.

Here is a version with HFS paths. It also doesn’t have the user folder hard-coded so will work on any account.

local source, destination
set source to path to library folder from user domain as text
set destination to path to documents folder as text
tell application id "com.apple.Finder"
	duplicate file (source & "Thunderbird:Profiles:7jyt1da7.default-release:abook.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Address Book Sync") with replacing
	
	duplicate file (source & "Thunderbird:Profiles:7jyt1da7.default-release:history.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Address Book Sync") with replacing
	
	duplicate file (source & "Application Support:Firefox:Profiles:jla842kt.default-release:bookmarks.html") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate file (source & "Application Support:Firefox:Profiles:jla842kt.default-release:favicons.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate file (source & "Application Support:Firefox:Profiles:jla842kt.default-release:places.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate file (source & "Application Support:ChronoSync:Tasks") ¬
		to folder (destination & "MacBook Pro Specific:Backup Scripts:ChronoSync Tasks") with replacing
	
	duplicate file (source & "Application Support:XMenu:Custom") ¬
		to folder (destination & "MacBook Pro Specific:XMenu Sync") with replacing
	
	duplicate file (source & "Application Support:SpamSieve") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup") with replacing
	
	duplicate file (source & "Preferences:com.c-command.SpamSieve.plist") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup:Preference File") with replacing
	
	duplicate file (source & "Application Scripts:com.apple.mail:Apple Mail - Remote Training.scpt") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup:AppleScript") with replacing
end tell
display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

Here is a version with loops thru lists that contain source & destination files/folders

local sourceBase, destinationBase, sourcelist, destinationList, source, destination, i, j
set sourceBase to path to library folder from user domain as text
set destinationBase to path to documents folder as text
set sourcelist to {{"Thunderbird:Profiles:7jyt1da7.default-release:", {"abook.sqlite", "history.sqlite"}}, {"Application Support:Firefox:Profiles:jla842kt.default-release:", {"bookmarks.html", "favicons.sqlite", "places.sqlite"}}, {"Application Support:ChronoSync:", {"Tasks"}}, {"Application Support:XMenu:", {"Custom"}}, {"Application Support:", {"SpamSieve"}}, {"Preferences:", {"com.c-command.SpamSieve.plist"}}, {"Application Scripts:com.apple.mail:", {"Apple Mail - Remote Training.scpt"}}}
set destinationList to {"MacBook Pro Specific:Address Book Sync", "MacBook Pro Specific:Firefox Bookmarks Sync", "MacBook Pro Specific:Backup Scripts:ChronoSync Tasks", "MacBook Pro Specific:XMenu Sync", "MacBook Pro Specific:SpamSieve Backup", "MacBook Pro Specific:SpamSieve Backup:Preference File", "MacBook Pro Specific:SpamSieve Backup:AppleScript"}
tell application id "com.apple.Finder"
	repeat with i from 1 to count sourcelist
		set sourceItem to item i of sourcelist
		set destination to item i of destinationList
		repeat with j from 1 to count item 2 of sourceItem
			set source to sourceBase & (item 1 of sourceItem) & (item j of item 2 of sourceItem)
			try
				duplicate file source to folder destination with replacing
			end try
		end repeat
	end repeat
end tell
display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

Robert, thank you for both above scripts. This will give me the opportunity to not only try them, but study how it is that they work. Then, if I’m lucky, I’ll be able to modify them to work on my 2018 Intel Mac mini, in which the user account has a different name, and of course the “MacBook Pro Specific” will also change to “Mac Mini Specific”. Again, thanks for all the scripts to study and review.

I could have also put the destinations in the same list with the sources, but I chose not to for readability

Thought I’d try working with this one first (version with HFS paths), as the changes from my MacBook Pro to my 2018 Intel Mac mini would be minor. But, on the MacBook Pro, just copying this script in Script Debugger and running it, I get an error when it gets to the ChronoSync tasks item (see screenshot). I’ve checked and rechecked the paths and they are correct. I’m stumped.

Figured it out, had to change certain “duplicate file” entries to “duplicate folder” entries, and it now functions perfectly. Learning experience, changes shown below.

local source, destination
set source to path to library folder from user domain as text
set destination to path to documents folder as text
tell application id "com.apple.Finder"
	duplicate file (source & "Thunderbird:Profiles:7jyt1da7.default-release:abook.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Address Book Sync") with replacing
	
	duplicate file (source & "Thunderbird:Profiles:7jyt1da7.default-release:history.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Address Book Sync") with replacing
	
	duplicate file (source & "Application Support:Firefox:Profiles:jla842kt.default-release:bookmarks.html") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate file (source & "Application Support:Firefox:Profiles:jla842kt.default-release:favicons.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate file (source & "Application Support:Firefox:Profiles:jla842kt.default-release:places.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate folder (source & "Application Support:ChronoSync:Tasks") ¬
		to folder (destination & "MacBook Pro Specific:Backup Scripts:ChronoSync Tasks") with replacing
	
	duplicate folder (source & "Application Support:XMenu:Custom") ¬
		to folder (destination & "MacBook Pro Specific:XMenu Sync") with replacing
	
	duplicate folder (source & "Application Support:SpamSieve") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup") with replacing
	
	duplicate file (source & "Preferences:com.c-command.SpamSieve.plist") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup:Preference File") with replacing
	
	duplicate file (source & "Application Scripts:com.apple.mail:Apple Mail - Remote Training.scpt") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup:AppleScript") with replacing
end tell
display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

I re-did my looping version to fix the folder issue.
I made sure folder paths in the lists end with a colon (“:”)

local sourceBase, destinationBase, sourcelist, destinationList, source, destination, i, j
set sourceBase to path to library folder from user domain as text
set destinationBase to path to documents folder as text
set sourcelist to {{"Thunderbird:Profiles:7jyt1da7.default-release:", {"abook.sqlite", "history.sqlite"}}, {"Application Support:Firefox:Profiles:jla842kt.default-release:", {"bookmarks.html", "favicons.sqlite", "places.sqlite"}}, {"Application Support:ChronoSync:", {"Tasks:"}}, {"Application Support:XMenu:", {"Custom:"}}, {"Application Support:", {"SpamSieve:"}}, {"Preferences:", {"com.c-command.SpamSieve.plist"}}, {"Application Scripts:com.apple.mail:", {"Apple Mail - Remote Training.scpt"}}}
set destinationList to {"MacBook Pro Specific:Address Book Sync:", "MacBook Pro Specific:Firefox Bookmarks Sync:", "MacBook Pro Specific:Backup Scripts:ChronoSync Tasks:", "MacBook Pro Specific:XMenu Sync:", "MacBook Pro Specific:SpamSieve Backup:", "MacBook Pro Specific:SpamSieve Backup:Preference File:", "MacBook Pro Specific:SpamSieve Backup:AppleScript:"}
tell application id "com.apple.Finder"
	repeat with i from 1 to count sourcelist
		set sourceItem to item i of sourcelist
		set destination to item i of destinationList
		repeat with j from 1 to count item 2 of sourceItem
			set source to sourceBase & (item 1 of sourceItem) & (item j of item 2 of sourceItem)
			try
				if source ends with ":" then -- its a folder
					duplicate folder source to folder destination with replacing
				else
					duplicate file source to folder destination with replacing
				end if
			end try
		end repeat
	end repeat
end tell
display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

Thanks Robert, I don’t think I would have figured this one out. I did run the original one a couple of times and would finally kill it after it went more than two minutes with no results. I’ll try the new version over coffee tomorrow morning.

Robert, ran the looping script a couple of times this morning, and both times it went 2 to 3 minutes before I ended the run. No files/folders copied/pasted.

While I appreciate your efforts on this, I now have two workable solutions, my original (which you cleaned up) and the HFS version which works equally well. I’m sure you have much better things to do other than trouble shoot a script. Many thanks for all your help with this, and things in the past that I’ve struggled with.

Hi.

folder and file are both subclasses of the Finder’s item class, so item covers them both:

duplicate item source to folder destination with replacing

Just so I am clear on your post, are you saying that the script that I posted above could have been written as so?

local source, destination
set source to path to library folder from user domain as text
set destination to path to documents folder as text
tell application id "com.apple.Finder"
	duplicate item (source & "Thunderbird:Profiles:7jyt1da7.default-release:abook.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Address Book Sync") with replacing
	
	duplicate item (source & "Thunderbird:Profiles:7jyt1da7.default-release:history.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Address Book Sync") with replacing
	
	duplicate item (source & "Application Support:Firefox:Profiles:jla842kt.default-release:bookmarks.html") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate item (source & "Application Support:Firefox:Profiles:jla842kt.default-release:favicons.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate item (source & "Application Support:Firefox:Profiles:jla842kt.default-release:places.sqlite") ¬
		to folder (destination & "MacBook Pro Specific:Firefox Bookmarks Sync") with replacing
	
	duplicate item (source & "Application Support:ChronoSync:Tasks") ¬
		to folder (destination & "MacBook Pro Specific:Backup Scripts:ChronoSync Tasks") with replacing
	
	duplicate item (source & "Application Support:XMenu:Custom") ¬
		to folder (destination & "MacBook Pro Specific:XMenu Sync") with replacing
	
	duplicate item (source & "Application Support:SpamSieve") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup") with replacing
	
	duplicate item (source & "Preferences:com.c-command.SpamSieve.plist") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup:Preference File") with replacing
	
	duplicate item (source & "Application Scripts:com.apple.mail:Apple Mail - Remote Training.scpt") ¬
		to folder (destination & "MacBook Pro Specific:SpamSieve Backup:AppleScript") with replacing
end tell
display notification "Completed Successfully!" with title "Pre Backup AppleScript"

delay 1

Hi @Homer712.

Yes, that’s right. In the Finder, an item can be a file or a folder or a disk. Your script does the same thing with both files and folders, so you may as well refer to them as items. Then you can use a compact repeat like Robert’s above, but without having to cater for what the items actually are.