Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator - Copy multiple filenames without extensions as list

Hi,


Looking for support to create an Automator / Apple Script to copy filenames to the clipboard as a list. The end result cannot have commas, or spaces. Any advice is appreciated!


Mac Pro

Posted on Sep 14, 2021 7:53 PM

Reply

Similar questions

3 replies

Sep 15, 2021 2:46 PM in response to PennyAllaVodka

Here is another approach at AppleScript/Objective-C where files selected in the Finder are reduced to their basename without any comma, period, or space in the filenames. Those characters are replaced by an underscore. The list of basenames are added to a mutable array where they are sorted alphabetically and then placed on the clipboard as separate line entities, ready to be pasted elsewhere as text:



Tested on macOS 11.6


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 NSCharacterSet : a reference to current application's NSCharacterSet

# initialize a mutable array
set marray to NSArray's array()'s mutableCopy()

# these characters will be replaced in the file's baseName by an underscore later
set notChars to NSCharacterSet's characterSetWithCharactersInString:"., "

tell application "Finder"
	
	if (get selection) = {} then
		display alert "You have not selected any Finder items"
		return
	end if
	
	set theFiles to (get selection as alias list)
	
	repeat with afile in theFiles
		set pafile to (POSIX path of afile) as text
		set baseName to (NSString's stringWithString:pafile)'s lastPathComponent()'s stringByDeletingPathExtension()
		
		# replace any undesireable characters in baseName with underscores
		set cleaned_baseName to ((baseName's componentsSeparatedByCharactersInSet:notChars)'s componentsJoinedByString:"_")
		
		set baseName to cleaned_baseName
		(marray's addObject:baseName)
	end repeat
end tell

# sort the mutable array ascending in place
marray's sortUsingSelector:{("localizedCaseInsensitiveCompare:")}

# make a text string of array items with these line endings
set theList to (marray's componentsJoinedByString:linefeed) as text

# In Finder Edit menu, you can select Show Clipboard and see this list of sorted filenames
set the clipboard to theList
return


Sep 14, 2021 10:55 PM in response to PennyAllaVodka

There are a couple of ways of doing this.


Arguable, the 'right' way is via AppleScriptObjC, which properly understands filenames, extensions, etc., and is faster than trying to manipulate the Finder to give you the result:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set list_of_filenames to {}
tell application "Finder"
	set theFiles to (get selection) 
	repeat with each_file in theFiles
		set posix_path to (POSIX path of (each_file as alias))
		set baseName to (current application's NSURL's fileURLWithPath:posix_path)'s URLByDeletingPathExtension()'s lastPathComponent() as text
		copy baseName to end of list_of_filenames
	end repeat
	return list_of_filenames
end tell


As for your 'can't have commas or spaces' requirement, that's a whole different story. What happens with a filename "some very long filename.txt" or "January 15th, 2020"? Do you want to strip the spaces and commas from that, too?

Automator - Copy multiple filenames without extensions as list

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