hi, I’m sure there’s a post somewhere on the forum on this topic but I can’t find it!
I’m looking for a simple script to copy a file name from Finder.
I would appreciate your expertise…
hi, I’m sure there’s a post somewhere on the forum on this topic but I can’t find it!
I’m looking for a simple script to copy a file name from Finder.
I would appreciate your expertise…
Hi,
try this, it takes the name of the first selected file
try
tell application "Finder" to set theName to name of item 1 of (get selection)
set the clipboard to theName
end try
thanks Stefan, perfect and quick as usual!
Just for the fun:
try
tell application "Finder" to set the clipboard to (get name of item 1 of (get selection))
end try
Yvan KOENIG (from FRANCE vendredi 20 février 2009 22:38:11)
merci Yvan
will your script save to clipboard multiple files’ name?
if not, what would be the way to obtain this?
thanks
this considers all items of the selection
try
set theNames to {}
tell application "Finder"
repeat with i in (get selection)
set end of theNames to name of i
end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
end try
thanks Stefan, that’s even better for my workflow!
Hi,
this works fine for me too. I use it in a MacOS service.
I have another question:
I want to use this script in a folder action in automator.
I want to get the filenames that I copy to that folder.
But when I use this script, it doesn’t copy the filenames but the folder name, where the files come from.
Is there a possibility to copy the copied files names?
Thanks a lot!
You may try :
on adding folder items to this_folder after receiving these_items
my germaine(this_folder, these_items)
end adding folder items to
on germaine(this_folder, the_items)
try
set theNames to {}
tell application "Finder"
repeat with i in the_items
set end of theNames to name of i
end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
end try
end germaine
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 31 octobre 2016 18:07:43
Hi,
cool!
I’m on Windows at the moment, but I will try it on Wednesday.
Thanks a lot!
Michi
Hi all Geniuses,
Im borrowing this thread a little bit, hope it’s okay.
I wonder if its possible to select a part of the filename and copy it to the clipboard.
For example :
I have a pdf document that is named: “Invoice_invoicenumber_clientid.pdf”
the word “Invoice_” will be static in all pdf-documents.
the word “invoicenumber” will be a different number for each pdf-document.
the word “clientid” will also be a different number for each pdf-document.
The part that I would like to select is the “clientid” in the example above. It will be added in the end of the filename, between “.pdf” and “_” (as separators). Can that be used as boundaries in the apple-script code to identify the clientid in the filename?
Thanks,
Niklas / Newbie
You may try :
set theFileName to "Invoice_invoicenumber_clientid.pdf"
set subString to item -2 of my decoupe(theFileName, {"_", ".pdf"})
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
Here is an alternate way using ASObjC features
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set theFileName to "Invoice_invoicenumber_clientid.pdf"
set subString to item -1 of (my splitWithoutExtension:theFileName usingString:"_")
#=====
on splitWithoutExtension:sourceString usingString:d1
set pathNSString to current application's NSString's stringWithString:sourceString
set sourceString to (pathNSString's stringByDeletingPathExtension)
return (sourceString's componentsSeparatedByString:d1) as list
end splitWithoutExtension:usingString:
I’m wondering if there is a way to use ASObjC with several delimiters.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 1 novembre 2016 11:25:49
There sort of is (componentsSeparatedByCharactersInSet:), but I don’t think it’s going to buy anything here.
It’s more efficient to stick with AS if you’re starting with AS text, want an AS text result, and the process is very simple.
set theFileName to "Invoice_invoicenumber_clientid.pdf"
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_", ".pdf"}
set the clipboard to text item -2 of theFileName
set AppleScript's text item delimiters to astid
Hello Shane .
I found componentsSeparatedByCharactersInSet but when I tried to use it my attempt failed.
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set theFileName to "Invoice_invoicenumber_clientid.pdf"
my splitWithDelims:theFileName usingStrings:{"_", "."}
--set subString to item -2 of result
#=====
on splitWithDelims:sourceString usingStrings:theDelims
# makes an NSString from the string
set pathNSString to current application's NSString's stringWithString:sourceString
# makes an NSSet from the list of delims
set setOfDelims to current application's NSSet's setWithArray:theDelims
return (pathNSString's componentsSeparatedByCharactersInSet:setOfDelims) as list
--> error "-[__NSSetI _expandedCFCharacterSet]: unrecognized selector sent to instance 0x618002447440" number -10000
end splitWithDelims:usingStrings:
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 1 novembre 2016 16:07:05
Hi Yvan.
The method to which Shane referred requires an NSCharacterSet. It could be used in this case, where both delimiters are single characters.
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set theFileName to "Invoice_invoicenumber_clientid.pdf"
my splitWithDelims:theFileName usingStrings:{"_", "."}
--set subString to item -2 of result
#=====
on splitWithDelims:sourceString usingStrings:theDelims
# makes an NSString from the string
set pathNSString to current application's NSString's stringWithString:sourceString
# makes an NSString from the list of delims and an NSCharacterSet from that
set theDelims to (current application's NSArray's arrayWithArray:theDelims)'s componentsJoinedByString:""
set setOfDelims to current application's NSCharacterSet's characterSetWithCharactersInString:theDelims
return (pathNSString's componentsSeparatedByCharactersInSet:setOfDelims) as list
--> {"Invoice", "invoicenumber", "clientid", "pdf"}
end splitWithDelims:usingStrings:
Thanks Nigel
This time I would have found that by myself. It seems that I wasn’t fully awake (or already asleep).
Given the posted infos I built that :
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
on replaceSeveral:sourceString existingStrings:listOfStrings newString:d1
set sourceString to current application's NSString's stringWithString:sourceString
repeat with d0 in listOfStrings
set sourceString to (sourceString's stringByReplacingOccurrencesOfString:d0 withString:d1)
end repeat
return sourceString as text
end replaceSeveral:existingStrings:newString:
on replaceSeveralCharacters:sourceString existingChars:listOfChars newString:d1
set sourceString to current application's NSString's stringWithString:sourceString
set theDelims to (current application's NSArray's arrayWithArray:listOfChars)'s componentsJoinedByString:""
set setOfDelims to current application's NSCharacterSet's characterSetWithCharactersInString:theDelims
set anArray to (sourceString's componentsSeparatedByCharactersInSet:setOfDelims)
return (anArray's componentsJoinedByString:d1) as text
end replaceSeveralCharacters:existingChars:newString:
my replaceSeveral:"juste pour « voir »" existingStrings:{"« ", " »"} newString:quote
log result --> (*juste pour "voir"*)
my replaceSeveralCharacters:"just for "see"" existingChars:{""", """} newString:quote
log result --> (*just for "see"*)
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 1 novembre 2016 19:24:47
Hi,
it seems that the filenames are not copied to the clipboard. But I can’t be sure, because i have quiet no idea of apple script.
I created a folder action in automator. Firest step is the script, second step is a “copy from clipboard” action, third is “new mail” and the last one is “send mail”.
Then I right click on a test folder on the desktop and select “Folder action…”. In the new window I select the folder on the left side and the automator workflow on the right side.
When I drop some files in this folder, the email will be send but not with the copied filenames in it. If I copy something else to the clipboard before, this text will be inserted in the email.
Any ideas?
Thanks a lot,
Michi
I never use Automator.
As I tested it, I just know that the posted script does the job.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) jeudi 3 novembre 2016 16:13:52
For the record: set the clipboard to command shouldn’t be in a tell application Finder block. It causes an error and is bounced back to current application.