AppleScripts doesn't work on Sonoma

Is there a trick to using AppleScript on Sonoma?
I have tried repeatedly to add text to filenames but all i get is a double grunt from AS and no changes.

Please post your script

Richard. AppleScript works fine on Sonoma, although it has a number of bugs and perhaps you’ve encountered one of those. Just by way of example, the following works on my Sonoma computer.

set theFile to (choose file) --> alias "Macintosh HD:Users:Robert:Working:Test File.txt"

tell application "Finder" to set fileName to name of theFile --> "Test File.txt"

set newName to "Peavine " & fileName --> "Peavine Test File.txt"

As Robert has suggested, we can better help if you post your script.

None of the Finder or Folder scripts work.

(*

Replace Text In Item Names

This script is designed to replace text in the names of specific files and/or folders in the front window of the desktop.

If no folder windows are open, the script will affect files and/or folders on the desktop.

Copyright © 2001–2007 Apple Inc.

You may incorporate this Apple sample code into your program(s) without

restriction. This Apple sample code has been provided “AS IS” and the

responsibility for its operation is yours. You are not permitted to

redistribute this Apple sample code as “Apple sample code” after having

made changes. If you’re going to redistribute the code, we require

that you make it clear that the code was descended from Apple sample

code, but that you’ve made changes.

*)

–set the source_folder to choose folder with prompt “Folder containing items to edit:”

– get the path to the folder of the front window

– if no windows are open, the desktop folder will be used

try

tell application “Finder” to set the source_folder to (folder of the front window) as alias

on error – no open folder windows

set the source_folder to path to desktop folder as alias

end try

display dialog “Search and replace in:” buttons {“File Names”, “Folder Names”, “Both”} default button 3

set the search_parameter to the button returned of the result

repeat

display dialog “Enter text to find in the item names:” default answer “” buttons {“Cancel”, “OK”} default button 2

set the search_string to the text returned of the result

if the search_string is not “” then exit repeat

end repeat

repeat

display dialog “Enter replacement text:” default answer “” buttons {“Cancel”, “OK”} default button 2

set the replacement_string to the text returned of the result

if the replacement_string contains “:” then

beep

display dialog “A file or folder name cannot contain a colon (:).” buttons {“Cancel”, “OK”} default button 2

else if the replacement_string contains “/” then

beep

display dialog “A file or folder name cannot contain a forward slash (/).” buttons {“Cancel”, “OK”} default button 2

else

exit repeat

end if

end repeat

display dialog “Replace “” & the search_string & “” with “” & the replacement_string & “” in every item name?” buttons {“Cancel”, “OK”} default button 2

set the item_list to list folder source_folder without invisibles

set source_folder to source_folder as string

repeat with i from 1 to number of items in the item_list

set this_item to item i of the item_list

set this_item to (source_folder & this_item) as alias

set this_info to info for this_item

set the current_name to the name of this_info

set change_flag to false

if the current_name contains the search_string then

if the search_parameter is “Folder Names” and ¬

folder of this_info is true then

set the change_flag to true

else if the search_parameter is “File Names” and ¬

folder of this_info is false then

set the change_flag to true

else if the search_parameter is “Both” then

set the change_flag to true

end if

if the change_flag is true then

– replace target string using delimiters

set AppleScript’s text item delimiters to the search_string

set the text_item_list to every text item of the current_name

set AppleScript’s text item delimiters to the replacement_string

set the new_item_name to the text_item_list as string

set AppleScript’s text item delimiters to “”

my set_item_name(this_item, new_item_name)

end if

end if

end repeat

beep 2

on set_item_name(this_item, new_item_name)

tell application “Finder”

–activate

set the parent_container_path to (the container of this_item) as text

if not (exists item (the parent_container_path & new_item_name)) then

try

set the name of this_item to new_item_name

on error the error_message number the error_number

if the error_number is -59 then

set the error_message to “This name contains improper characters, such as a colon (:).”

else --the suggested name is too long

set the error_message to error_message – “The name is more than 31 characters long.”

end if

–beep

tell me to display dialog the error_message default answer new_item_name buttons {“Cancel”, “Skip”, “OK”} default button 3

copy the result as list to {new_item_name, button_pressed}

if the button_pressed is “Skip” then return 0

my set_item_name(this_item, new_item_name)

end try

else --the name already exists

–beep

tell me to display dialog “This name is already taken, please rename.” default answer new_item_name buttons {“Cancel”, “Skip”, “OK”} default button 3

copy the result as list to {new_item_name, button_pressed}

if the button_pressed is “Skip” then return 0

my set_item_name(this_item, new_item_name)

end if

end tell

end set_item_name

Please learn how to post code in the forums properly

2 Likes

I corrected formatting errors and the script worked on my Sonoma computer. An example of the formatting errors is the use of left and right, single and double quotation marks. I didn’t review the code otherwise.

(*
Replace Text In Item Names
This script is designed to replace text in the names of specific files and/or folders in the front window of the desktop.
If no folder windows are open, the script will affect files and/or folders on the desktop.
Copyright © 2001–2007 Apple Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you’re going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you’ve made changes.
*)

--set the source_folder to choose folder with prompt "Folder containing items to edit:"
--get the path to the folder of the front window
--if no windows are open, the desktop folder will be used

try
	tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
	set the source_folder to path to desktop folder as alias
end try

display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
set the search_parameter to the button returned of the result

repeat
	display dialog "Enter text to find in the item names:" default answer "" buttons {"Cancel", "OK"} default button 2
	set the search_string to the text returned of the result
	if the search_string is not "" then exit repeat
end repeat

repeat
	display dialog "Enter replacement text:" default answer "" buttons {"Cancel", "OK"} default button 2
	set the replacement_string to the text returned of the result
	if the replacement_string contains ":" then
		beep
		display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} default button 2
	else if the replacement_string contains "/" then
		beep
		display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", "OK"} default button 2
	else
		exit repeat
	end if
end repeat

display dialog "Replace " & the search_string & " with " & the replacement_string & " in every item name?" buttons {"Cancel", "OK"} default button 2

set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string

repeat with i from 1 to number of items in the item_list
	set this_item to item i of the item_list
	set this_item to (source_folder & this_item) as alias
	set this_info to info for this_item
	set the current_name to the name of this_info
	set change_flag to false
	if the current_name contains the search_string then
		if the search_parameter is ("Folder Names" & folder of this_info is true) then
			set the change_flag to true
		else if the search_parameter is "File Names" and folder of this_info is false then
			set the change_flag to true
		else if the search_parameter is "Both" then
			set the change_flag to true
		end if
		if the change_flag is true then
			-- replace target string using delimiters
			set AppleScript's text item delimiters to the search_string
			set the text_item_list to every text item of the current_name
			set AppleScript's text item delimiters to the replacement_string
			set the new_item_name to the text_item_list as string
			set AppleScript's text item delimiters to ""
			my set_item_name(this_item, new_item_name)
		end if
	end if
end repeat

beep 2

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

BTW, to post scripts in this forum, you put three backticks on a separate line, then the script, and then three backticks on a separate line

Thanks

I would still like to know why the standard AppleScripts that came with my Mac do not work. None of them! Is there a switch to activate them?

AppleScript should work on Sonoma as is just like on any other system. You’ll need to give us specific examples of scripts that came with your Mac and explain how exactly they “do not work” (errors, your way of invoking the scripts etc.)

1 Like

P.S. As @peavine already pointed, the original script you posted could never be compiled due to incorrect formatting (- instead of -- used in comments, the use of curly quotes instead of straight ones, and potentially other formatting issues). Is it possible that you have the same issues with other scripts you have problems with?

The script was the script from the AS menu bar. This is what I do not understand. How did Sonoma come with scripts that do not work!

How can I get all the scripts that work. I do not want to go through all them manually.

Everyone keeps asking, but you will not answer correctly. We want an EXPLICIT example of a script that isn’t working. Post it, the entire script, not just the name of the script, along with the exact error you are getting. When I say post, I mean correctly so that it shows as a script, not a quote, because this forum software will replace quotes and other formatting so as not to be able to compile. Then maybe we can help resolve the issue.

4 Likes

None of the Applescripts work. I did not write any of them. They came with the Mac.

Richard. Thanks for posting the screenshot.

I can’t find those scripts on my Sonoma computer. Can you post the path to them? Is it possible they are from an earlier install of macOS and were not removed when Sonoma was installed?

My Scripts folder:

I think you’re on to something. I noticed the dates on them and they are from my previous version of MacOS.
How to I get updated ones.

Thank you very much

I’m not aware of any updated versions. If you don’t have the expertise to revise them, you can post a particular script or two (not all of them) in this forum, and a forum member will probably be able to make the necessary edits. When posting a script, please make sure that the line before and after the script contains three backtick characters.

In the meantime, check your Security and Privacy settings and see if there is any application asking for disk access or some other privilege.