Apple Script with M1 Chip

My MacBook Pro died and I replaced it with MacBook Air with M1 chip. I have a couple Apple Scripts to move files from between folders. The scripts do not work in the new MacBook. Is there something I need to do differently or is this a bug?


Thanks for any help.

John

MacBook Air (2020 or later)

Posted on May 12, 2022 4:38 AM

Reply
Question marked as Top-ranking reply

Posted on May 12, 2022 8:03 AM

I pasted your script in ScriptEditor.


I created a folder "A" with files "a" and "b".

I created an empty folder "B".


In my desktop I createad a text file with two lines with the names of the files.


I started your script, selected the file containing the list, selected the origin folder, selected the destination folder.


It worked as expected.


Note: the first thing I saw was a dialog saying that the script wanted to access files in the Desktop. I allowed it.

Without that, it would not have worked, of course.


So the script does work in Monterey. Now we need to try and figure out why it is not working for you.


Are you launching the script from Script Editor? From Automator? Or as an application on its own?

Similar questions

39 replies
Question marked as Top-ranking reply

May 12, 2022 8:03 AM in response to Luis Sequeira1

I pasted your script in ScriptEditor.


I created a folder "A" with files "a" and "b".

I created an empty folder "B".


In my desktop I createad a text file with two lines with the names of the files.


I started your script, selected the file containing the list, selected the origin folder, selected the destination folder.


It worked as expected.


Note: the first thing I saw was a dialog saying that the script wanted to access files in the Desktop. I allowed it.

Without that, it would not have worked, of course.


So the script does work in Monterey. Now we need to try and figure out why it is not working for you.


Are you launching the script from Script Editor? From Automator? Or as an application on its own?

May 12, 2022 10:21 AM in response to JB001

I've got it working. I copied and pasted the script into a new script in script editor and it works as planned.

Sometimes the "reboot" method is what is needed.


Thanks to all who worked on this for me.

If anyone has suggestions on how I can pull the list of images out of a tab delimited text file without having to sort, copy & past into a new text doc, I'm all ears.

May 15, 2022 4:22 AM in response to VikingOSX

IT WORKS!!!

I got desperate and decided to try some random crazy things. The one that worked was I copied the two column header names referenced in the script from the input file and pasted them in the script. It now works like a charm. Go figure.

Thank you, VikingOSX sooooo much for all your help. You are the best!

May 13, 2022 5:26 AM in response to JB001

Here is a new script that takes the rows of CSV content and makes a list from these individual files to match your infolder. Tested on macOS 12.3.1. Further reading: AppleScript Language Guide.


use framework "Foundation"
use AppleScript version "2.4" # Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSArray : a reference to current application's NSArray
property istext : {"public.text", "public.plain-text"}
property adesktop : (path to desktop) as alias

set moveList to {}
set matchList to {}

# the invisibles is true, but multiple selections allowed and show package folders are false by default
set myList to (choose file with prompt "Select file containing list:" of type istext default location adesktop without invisibles)
set inFolder to (choose folder with prompt "Select copy-from folder: " default location adesktop)
set outFolder to (choose folder with prompt "Select copy-to folder: " default location adesktop)

# read the text file into a list of the images to duplicate
set moveList to (read myList as text using delimiter linefeed) # text is utf-8 by default
# new list with all xsv file items
set updMoveList to my xsv_to_list(moveList, tab)

tell application "Finder"
	activate
	set matchList to (every item in folder inFolder whose name is in updMoveList) as alias list
	repeat with anItem in matchList
        # use replacing true and exact copy true to overwrite and retain attributes
		duplicate ((inFolder & anItem's name) as text) to folder outFolder with replacing and exact copy
	end repeat
end tell
return

on xsv_to_list(alist, delim)
	# make a new list from all row items in the xsv file. In this case the spaces are tabs
	# as read: {"abc" "def" "ghi", "jkl" "mno" "pqr"} to {"abc", "def", "ghi", "jkl", ...}
	
	# make mutable string and array objects
	set str to NSString's alloc()'s init()'s mutableCopy()
	set newList to NSArray's array()'s mutableCopy()
	
	repeat with arow in alist
		(str's setString:arow)
		(newList's addObjectsFromArray:(str's componentsSeparatedByString:delim))
	end repeat
	return (newList) as list
end xsv_to_list


May 14, 2022 5:50 AM in response to JB001

The simple reason it wasn't working for one "R" entry is that it was entered with a trailing space in the Retouch column. I now trim spaces from either side of the "R" and the code works correctly.


I still recommend you test this before putting it into production, but from my test data, it seems to work correctly.


(*
	retouch.applescript
	
	Process a tab-separated-values (.tsv) file of 23 columns and several hundred records to identify
	"R" in the Retouch column, and add its associated filename for retouching to a list. That list
	then attempts to match filenames in a selected folder with the same name, and duplicate those
	files to an outfolder for subsequent retouching.
	
	Reference: https://discussions.apple.com/thread/253890579
	Tested: macOS 12.3.1 on a 2021 16-in MBP M1 Pro
	VikingOSX, 2022-05-14, Apple Support Communities, No warranties expressed or implied.
*)

use framework "Foundation"
use AppleScript version "2.4" # Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSCharacterSet : a reference to current application's NSCharacterSet
property NSArray : a reference to current application's NSArray
property istext : {"public.text", "public.plain-text"}
property adesktop : (path to desktop) as alias
property delim : tab

set moveList to {}
set matchList to {}
set retouchList to NSArray's array()'s mutableCopy()

-- by default, the invisibles is true, and multiple selections allowed and show package folders are false
set myList to (choose file with prompt "Select file containing list:" of type istext default location adesktop without invisibles)
set inFolder to (choose folder with prompt "Select copy-from folder: " default location adesktop)
set outFolder to (choose folder with prompt "Select copy-to folder: " default location adesktop)

-- read the text file into a list of the images to duplicate
set moveList to (read myList as text using delimiter linefeed) -- text is utf-8 by default
-- extract just the header fields
set headerList to ((NSString's stringWithString:(item 1 of moveList))'s componentsSeparatedByString:delim)
set dataList to rest of moveList

-- get column number representing header fields of interest
-- we add 1 because Objective-C arrays are zero-based
set retouch to (headerList's indexOfObject:"Retouch") + 1
set fname to (headerList's indexOfObject:"Filename") + 1

repeat with arow in dataList
	-- hunt for "R" in Retouch column from this row of data
	set x to (my retouch_files_list(retouch, arow, delim)) as text
	-- and if found, then add the associated filename to the retouchList array
	if my to_uppercase(x) = "R" then
		(retouchList's addObject:(my retouch_files_list(fname, arow, delim)))
	end if
end repeat

-- convert the NSMutableArray back to an AppleScript list
set retouchList to retouchList as list

if (count of (retouchList as list)) = 0 then
	display alert "No Retouch Items were found… quitting."
	return
end if

tell application "Finder"
	activate
	set matchList to (every item in folder inFolder whose name is in retouchList) as alias list
	repeat with anItem in matchList
		-- use replacing true and exact copy true to overwrite and retain attributes
		duplicate ((inFolder & anItem's name) as text) to folder outFolder with replacing and exact copy
	end repeat
end tell

-- cleanup
repeat with alist in {moveList, matchList, retouchList, headerList, dataList}
	set alist to {}
end repeat

display dialog "Script is done, check your Outfolder for results."
return

on retouch_files_list(fieldndx, astr, delim)
	return (item fieldndx of (my txt_to_list(astr, delim)) as text)
end retouch_files_list

on txt_to_list(astr, delim)
	return ((NSString's stringWithString:astr)'s componentsSeparatedByString:delim) as list
end txt_to_list

on to_uppercase(astr)
	-- force to uppercase and remove surrounding whitespace if present
	set nows to NSCharacterSet's whitespaceCharacterSet
	return ((NSString's stringWithString:astr)'s localizedUppercaseString()'s stringByTrimmingCharactersInSet:nows) as text
end to_uppercase


May 15, 2022 9:28 AM in response to JB001

Got more sleep last night and while on the road this morning, realized that the code that I had provided earlier was doing redundant data record processing. I have fixed that with code revision and it tests for me just as before. Whatever you changed to make it work with your specific data will need to be done again though.


(*
	retouch.applescript
	
	Process a tab-separated-values (.tsv) file of 23 columns and several hundred records to identify
	"R" in the Retouch column, and add its associated filename for retouching to a list. That list
	then attempts to match filenames in a selected folder with the same name, and duplicate those
	files to an outfolder for subsequent retouching.
	
	Reference: https://discussions.apple.com/thread/253890579
	Tested: macOS 12.3.1 on a 2021 16-in MBP M1 Pro
	Updated: 2022-05-15, removed text to list code redundancy
	VikingOSX, 2022-05-14, Apple Support Communities, No warranties expressed or implied.
*)

use framework "Foundation"
use AppleScript version "2.4" # Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSCharacterSet : a reference to current application's NSCharacterSet
property NSArray : a reference to current application's NSArray
property istext : {"public.text", "public.plain-text"}
property adesktop : (path to desktop) as alias
property delim : tab

set moveList to {}
set rowItems to {}
set matchList to {}
set retouchList to NSArray's array()'s mutableCopy()

-- by default, the invisibles is true, and other clauses are false by default
set myList to (choose file with prompt "Select file containing list:" of type istext default location adesktop without invisibles)
set inFolder to (choose folder with prompt "Select copy-from folder: " default location adesktop)
set outFolder to (choose folder with prompt "Select copy-to folder: " default location adesktop)

-- read the text file into a list of the images to duplicate
-- all 23 fields in a row are string list items
set moveList to (read myList as text using delimiter linefeed) -- text is utf-8 by default
-- extract just the header fields
set headerList to ((NSString's stringWithString:(item 1 of moveList))'s componentsSeparatedByString:delim)
-- and then the rows of data
set dataList to rest of moveList

-- get column number representing header fields of interest
-- assumption: Header fields are not already double-quoted
-- we add 1 because Objective-C arrays are zero-based
set retouch to (headerList's indexOfObject:"Retouch") + 1
set fname to (headerList's indexOfObject:"Filename") + 1

-- process data rows
repeat with arow in dataList
	-- convert the row as text to individual list items so we can find retouch and filename content
	set rowItems to my txt_to_list(arow, delim)
	-- retrieve retouch code if present in the row items
	set rcode to (item retouch of rowItems) as text
	-- and if found, then add the associated filename to the retouchList array
	if my to_uppercase(rcode) = "R" then
		(retouchList's addObject:((item fname of rowItems) as text))
	end if
end repeat

-- convert the NSMutableArray back to an AppleScript list
set retouchList to retouchList as list

if (count of (retouchList as list)) = 0 then
	display alert "No Retouch Items were found… quitting."
	return
end if

tell application "Finder"
	activate
	set matchList to (every item in folder inFolder whose name is in retouchList) as alias list
	repeat with anItem in matchList
		-- use replacing true and exact copy true to overwrite and retain attributes
		duplicate ((inFolder & anItem's name) as text) to folder outFolder with replacing and exact copy
	end repeat
end tell

-- cleanup
repeat with alist in {moveList, rowItems, matchList, retouchList, headerList, dataList}
	set alist to {}
end repeat

display dialog "Script is done, check your Outfolder for results."
return

on txt_to_list(astr, delim)
	-- split the text row to its individual list items based on TSV delimiter (tab)
	return ((NSString's stringWithString:astr)'s componentsSeparatedByString:delim) as list
end txt_to_list

on to_uppercase(astr)
	-- force to uppercase and remove surrounding whitespace if present
	set nows to NSCharacterSet's whitespaceCharacterSet
	return ((NSString's stringWithString:astr)'s localizedUppercaseString()'s stringByTrimmingCharactersInSet:nows) as text
end to_uppercase


May 12, 2022 6:40 AM in response to VikingOSX

Here is what I have been using. We send a list to our photo lab and create galleries for students to order photos. The lab returns our list with info including who ordered and which image they selected. Some of the orders select our touch up service. We sort the file received from the lab and copy the filenames for the images to be touched up. That list is pasted into a plain text file used to copy those images to a new folder. The images are touched up and put back into the original folder.

At some point I want to figure out how to do this directly from the file received from the lab but right not that is over my head ;-)


<


property istext : {"public.text", "public.plain-text"}

property adesktop : (path to desktop) as alias

set moveList to {}

set matchList to {}

set myList to (choose file with prompt "Select file containing list:" of type istext default location adesktop without invisibles, multiple selections allowed and showing package contents)

set inFolder to (choose folder with prompt "Select copy-from folder: " default location adesktop without invisibles, multiple selections allowed and showing package contents)

set outFolder to (choose folder with prompt "Select copy-to folder: " default location adesktop without invisibles, multiple selections allowed and showing package contents)

# read the text file into a list of the images to duplicate

set moveList to read myList as «class utf8» using delimiter linefeed

tell application "Finder"

set matchList to (every file in folder inFolder whose name is in moveList) as alias list

repeat with anItem in matchList

duplicate anItem to folder outFolder

end repeat


end tell>

May 12, 2022 8:23 AM in response to JB001

The correct usage of that code <> tool is to click once, and then copy/paste your code into the light gray field, press return after your last line of code, and then click the <> tool again to end the code insertion. You do not surround your posted code with explicit angle brackets.


I see no issues with the existing code when tested on macOS 12.3.1.

May 12, 2022 9:01 AM in response to Luis Sequeira1

Yes, it works on both a iMac and MacBook Pro running Monterey. I hit the problem when I tried running it on a MacBook Running Monterey with M1 chip.

I've tried running it by launching from the Script Editor and as an application. Same results -- it does nothing. That's what made me think I needed to do something different with that M1 chip, but don't know what.

May 13, 2022 6:13 AM in response to VikingOSX

Wow! Thank you VikingOSX. I'm sure someone with your experience finds this easy but for a newbie like me it is very helpful in trying understand this new world and its capabilities. I ran this using a test file containing all 23 fields we receive from the lab and it worked like a charm.

The file we receive usually has about 700 -800 records. What I ultimately need to do is only pull those images that are to be retouched. The input file structure has 23 fields. The first record has the field names, all subsequent records are data. The field named "Retouch" has an R in it when retouch is requested and the field named "Filename" contains the file name I need to copy from the input folder. Is there a way to just pull the retouch image file names?

May 13, 2022 6:48 AM in response to JB001

Show me a sample header with its fields and a second data record that shows an example for a "Retouch" record. If I know which column number of the first record has the Retouch "R" and the column number with a filename, I can filter on those records for a list of retouch filenames.


Are there any blank data fields beneath the Retouch column?


May 13, 2022 9:46 AM in response to VikingOSX

I am waiting for your header column and corresponding data columns for futher testing.


I created another tab delineated CSV with a header row of several names including Retouch and Filename. In the data fields. With some code revisions, I check if there is an "R" in the retouch column and add the corresponding filename to a list. I then use that list for name comparisions in your infolder selection and duplicate the matching files to the outfolder.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Script with M1 Chip

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.