A filelist of selection from finder suitable for use in Applescript

You can choose wether you want os9/hpfs style paths or posix path. The path is pasted to the clipboard.
Enjoy.

property produced : ""
set tids to AppleScript's text item delimiters 
set AppleScript's text item delimiters to ""
on makeList given posixStyle:boolean
	if posixStyle then
		local posixList
		set posixList to {}
		tell application "Finder"
			if selection ≠ "" then
				set filelist to (selection)
				repeat with anElm in filelist
					set posixList to posixList & POSIX path of (anElm as alias)
				end repeat
				set AppleScript's text item delimiters to return
				set produced to posixList as Unicode text
			else
				return
			end if
		end tell
	else
		local macFnStyleList
		set macFnStyleList to {}
		tell application "Finder"
			
			if selection ≠ "" then
				set theSel to (selection) 
				repeat with anElm in theSel
					set macFnStyleList to macFnStyleList & (anElm as alias)
				end repeat
				
				set AppleScript's text item delimiters to return
				
				set produced to macFnStyleList as Unicode text
			else
				return
			end if
		end tell
	end if
end makeList

makeList with posixStyle
display dialog "FileList to clipboard: as " default answer produced with title "FileList to clipboard" buttons { "Posix", "Os9"} default button 2 with icon 1
set the button_pressed to the button returned of the result
if the button_pressed is "Posix" then
	makeList with posixStyle
	set the clipboard to produced
else
	makeList without posixStyle
	set the clipboard to produced
end if
set AppleScript's text item delimiters to tids

Best regards

McUsr

I’m confused. Is this the same? I put it together in a minute.

on run
	set theclip to ""
	tell application "Finder" to set thefiles to (selection)
	set classas to button returned of (display dialog "What do you want the type of filepath to be?" buttons {"POSIX path", "HPFS path"})
	if classas is "POSIX path" then
		repeat with i from 1 to (count thefiles)
			if i is 1 then set n to ""
			if i > 1 then set n to " & "
			set theclip to theclip & n & ((item i of thefiles as alias)'s POSIX path)
		end repeat
	else
		repeat with i from 1 to (count thefiles)
			if i is 1 then set n to ""
			if i > 1 then set n to " & "
			set theclip to theclip & n & ((item i of thefiles as alias) as string)
		end repeat
	end if
	display dialog "\"" & theclip & "\"" & return & return & "Is this acceptable?" buttons {"Cancel", "Copy"} default button "Copy" cancel button "Cancel"
	set the clipboard to theclip
end run

Hello

I think it does the same. :slight_smile:

It is an early unrevised - old - version that just works which surely yours do as well.
Thought I’t might be handy, feel free to make a new post with yours and I’ll delete this.
I am not ashamed of it, but yours are way better and more efficient. -It was one of the first scripts I wrote.

Best Regards

McUsr

Hi, McUsr.

Thanks for posting your script. Just a few notes, though.

  1. The Mac OS file system is called “HFS+” (or “HFS Plus” or “Mac OS Extended”) and its paths are commonly known as “HFS paths”. It replaced Apple’s older HFS system as the Mac OS standard from Mac OS 8.1 and is still in use today. Full support for disks formatted the older HFS way only stopped with Mac OS 10.6, but they can still be read. HFS+ has nothing to do with HPFS and its path style isn’t peculiar to Mac OS 9.

  2. Your script has two lines of its implicit run handler written above the ‘makeList’ handler and the rest below it. For legibility, run handler code should be all together at one end of the script or the other.

2a. However, since the TIDs are only used inside the ‘makeList’ handler, those two particular lines and the one at the end of the script would be better inside the ‘makeList’ handler anyway.

  1. You don’t need to use a property. ‘makeList’ can return the text as a result which can be stored in a variable in the run handler or passed to the clipboard directly. (And since ‘makeList’ makes text and not a list, it might be stylistically nice to give it a less missleading label!)

  2. The ‘boolean’ you’ve put in makeList’s ‘given’ parameter is wrong. The thing after the colon should be the label of the variable you intend to use inside the handler. (It’s an idiosyncratic usage and I had to double-check it myself to make sure I got it right here.) For example:

on makeList given posixStyle:fred
	return fred
end makeList

makeList with posixStyle

Since you use the same label inside the handler, you need the same label on both sides of the colon:

on makeList given posixStyle:posixStyle
	--
end makeList
  1. Variables inside a handler are local by default. You only need to declare them local if the label’s also used for a property or a global elsewhere in the script.

  2. The Finder’s selection is never “”. When nothing’s selected, the selection’s an empty list. The script doesn’t need to check for this, as iterating through an empty list simply does nothing.

I’m using this for a long time


on run
	tell application "Finder" to set sel to selection
	if sel is {} then display dialog "Nothing selected" buttons {"Cancel"} default button 1
	open sel
end run

on open theseItems
	set {button returned:pathMode, gave up:gaveUP} to display dialog "Select the path type that you'd like copied to the Clipboard:" buttons {"HFS", "URL", "POSIX"} default button "POSIX" giving up after 2
	set home_folder to POSIX path of (path to home folder)
	set resultList to {}
	repeat with oneFile in theseItems
		set unixPath to POSIX path of (oneFile as alias)
		if pathMode is "POSIX" or gaveUP then
			if unixPath begins with home_folder then set unixPath to "~" & text (count home_folder) thru -1 of unixPath
			set end of resultList to unixPath
		else if pathMode is "URL" then
			tell application "Finder" to set end of resultList to URL of (oneFile as alias)
		else if pathMode is "HFS" then
			set end of resultList to oneFile as text
		end if
	end repeat
	
	set {TID, text item delimiters} to {text item delimiters, return}
	set resultList to resultList as text
	set text item delimiters to TID
	set the clipboard to resultList
end open

Hello

I have read your posts. And I must say that I’m most grateful for your comments and code sharing.
Nigel Garvey’s comments were both enlightening and useful, as the code from Dylan Weber and Stefan K
Thank you very much all of you. I’ll make a better version which I will post here.

Well, here it is: I couldn’t resist my self but threw in the opportunity to create an rtf link as well, as I sometimes need that. Knowing that it doesn’t exactly blend in with the title of the topic for this post, nor with the program logic.
But I like to have as few script menu items as I can, and since it is about the same functionality… I found this script to be an ok container for rtf-links as well. :slight_smile:

property l : {"Posix", "Hfs", "Url", "Rtf"}
property d : l's item 1 -- from Kai's dialog demo
global choice
tell application "Finder" to set sel to selection
if sel ≠ {} then
	tell application "System Events"
		tell (first application whose frontmost is true)
			activate
			set choice to (choose from list l default items d with prompt "Insert File identifier" OK button name {"Ok"})
			if choice is false then error number -128
		end tell
	end tell
else
	beep 2
	return
end if

set the clipboard to makeListOf(choice, sel)


on makeListOf(theWay, aliasList)
	set tids to AppleScript's text item delimiters
	
	set produced to {}
	set interrim to {}
	if theWay is {"Posix"} then
		repeat with anElm in aliasList
			set interrim to interrim & POSIX path of (anElm as alias)
			
		end repeat
		set AppleScript's text item delimiters to return
		set produced to interrim as Unicode text
	else if theWay is {"Hfs"} then
		repeat with anElm in aliasList
			set interrim to interrim & (anElm as alias)
		end repeat
		set AppleScript's text item delimiters to return
		set produced to interrim as Unicode text
	else if theWay is {"Url"} then -- thanks Stefan!
		repeat with anElm in aliasList
			tell application "Finder" to set interrim to interrim & URL of (anElm as alias)
		end repeat
		set AppleScript's text item delimiters to return
		set produced to interrim as Unicode text
	else if theWay is {"Rtf"} then
		if (count of aliasList) > 1 then
			display alert "Only one at a time."
		end if
		set interrim to rtfLink((item 1 of aliasList) as alias)
		set produced to interrim
	end if
	set AppleScript's text item delimiters to tids
	return produced
end makeListOf

on rtfLink(anAlias)
	-- totally stolen from http://www.macosxhints.com/article.php?story=20091002090934432
	-- I have a shell file - rather compact, but I can't figure out where that leading zero comes from.
	-- I'll come back and revise this one as well.
	local _msglnk
	set _linkText to extract_shortname_from(extract_filename_from(anAlias as text)) as text
	tell application "Finder" to set _msglnk to URL of (anAlias as alias)
	set startEcho to "echo "
	set echoDelimiter to "'"
	
	set html_1 to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n<title>"
	set html_2 to "</title>\n<meta name=\"Generator\" content=\"Cocoa HTML Writer\">\n<meta name=\"CocoaVersion\" content=\"1038.11\">\n<style type=\"text/css\">\np.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}\n</style>\n</head>\n<body>\n<p class=\"p1\"><a href=\""
	set html_3 to "\">"
	set html_4 to "</a></p>\n</body>\n</html>"
	
	set echoCommand to startEcho & echoDelimiter & html_1 & _linkText & html_2 & _msglnk & html_3 & _linkText & html_4 & echoDelimiter
	set textutilCommand to " | textutil -convert rtf -inputencoding UTF-8 -format html -stdin -stdout"
	set pbcopyCommand to " | pbcopy -Prefer rtf"
	
	set entireCommand to echoCommand & textutilCommand & pbcopyCommand
	
	do shell script entireCommand
	copy (the clipboard) to theResult
	return theResult
end rtfLink

-- AS SDK *tinkered* and *cloned* 
on extract_shortname_from(the_filepath) -- clone of one from the SDK made by me.
	set the_filepath to the_filepath as text
	try
		set x to the offset of "." in (the reverse of every character of the_filepath) as string
	on error
		return the_filepath
	end try
	return ((characters 1 thru -(x + 1) of the_filepath) as text)
end extract_shortname_from

on extract_filename_from(the_filepath) -- slightly modified since HFS-name of rtfd document ends with ":"
	local x
	set the_filepath to the_filepath as text
	set x to the offset of ":" in (the reverse of every character of the_filepath) as string
	if x is 1 then
		set the_filepath to characters 1 thru -2 of the_filepath as text
		return extract_filename_from(the_filepath)
	end if
	return ((characters -(x - 1) thru -1 of the_filepath) as text)
end extract_filename_from


Best Regards

McUsr