Choose File vs. Finder Selection

I’ve found code here in the forums and one of the initial lines is:

	tell application "Finder" to set sel to selection

Is there a way I can create an equivalent using Choose File?

	set sel to choose file with prompt "Please select the file to link:"

I don’t get the same results when I use the choose file structure which causes the script to generate the wrong data as a result.

I’d like to be able to run this on 10.6+.

Thanks.

Cheers,
Jon

Hi jon,

In the first case you’re getting a Finder reference and in the second case you’re getting an alias reference. You can use the alias reference at any time as compared to the Finder reference. Here’s a link to show you how to change the references:

http://macscripter.net/viewtopic.php?id=24575

gl,
kel

beside the file specifier difference the Finder selection returns a list and choose file returns a single item.
To get also a list you have to write


set sel to choose file with prompt "Please select the files to link:" with multiple selections allowed

Stefan & Kel,

Thank you both very much for the information. I’m working with it and having a new problem. When I transmogrify the alias reference from Choose File to a Finder reference, things start going awry and the modified script doesn’t work as intended.

I think if I post both original and modified you might catch my error(s).

Thanks.

Original Script from http://macscripter.net/viewtopic.php?id=38347 :

on run
	tell application "Finder" to set sel to selection
	open sel
end run

on SAR(main_text, search_text, replace_text)
	-- Search and Replace. Search for search-text in
	-- main_text and replace it by replace_text.
	set old_delims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to search_text
		considering case
			set parts to every text item of main_text
		end considering
		set AppleScript's text item delimiters to replace_text
		set newText to (parts as string)
	end try
	set AppleScript's text item delimiters to old_delims
	return newText
end SAR

on open theseItems
	
	## This Section Creates the Mac file path
	
	set MacServerPath to "file://afp.sally-fitch.com/"
	
	set macFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "/"
		set end of macFilePath to filePath as text
	end repeat
	set macFilePath to macFilePath as text
	set macFilePath1 to SAR(macFilePath, " ", "%20")
	
	## This Section Creates the windows file path
	
	set WinServerPath to "\\\\afp.sally-fitch.com\\"
	
	set winFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "\\"
		set end of winFilePath to filePath as text
	end repeat
	set winFilePath to winFilePath as text
	set winFilePath1 to SAR(winFilePath, " ", "%20")
	
	set the clipboard to "******************************************" & return & "Mac Path: " & return & MacServerPath & macFilePath1 & return & return & "If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k) " & return & return & "******************************************" & return & "Windows Path:" & return & WinServerPath & winFilePath1 & return & return & "If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer " & return & return & "******************************************"
end open


I’m trying to modify it to use Choose File and finally to paste the resulting link into Word, preferably as a hyperlink. I’ve found some sample code from Ben Waldie to try, but haven’t had the chance to integrate it into the script.

Using the suggestions you gave me, the proper format for the file is returned, but now the “open sel” line opens the file selected in the appropriate application rather than generating the link and I’m not sure why.

on run
	set LinkedFile to choose file with prompt "Please select the file to link:"
	--tell application "Finder" to set sel to selection
	set sel to item (LinkedFile as string) of application "Finder"
	
	open sel
end run

on SAR(main_text, search_text, replace_text)
	-- Search and Replace. Search for search-text in
	-- main_text and replace it by replace_text.
	set old_delims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to search_text
		considering case
			set parts to every text item of main_text
		end considering
		set AppleScript's text item delimiters to replace_text
		set newText to (parts as string)
	end try
	set AppleScript's text item delimiters to old_delims
	return newText
end SAR

on open theseItems
	
	## This Section Creates the Mac file path
	
	set MacServerPath to "file://afp.sally-fitch.com/"
	
	set macFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "/"
		set end of macFilePath to filePath as text
	end repeat
	set macFilePath to macFilePath as text
	set macFilePath1 to SAR(macFilePath, " ", "%20")
	
	
	set LinkText to MacServerPath & macFilePath1 & return
	set the clipboard to MacServerPath & macFilePath1 & return
	tell application "Microsoft Word"
		tell selection
			try
				set theClip to Unicode text of (the clipboard as record)
				type text text theClip
				--Would really like to have Word paste as hyperlink, but can't get syntax correct
			end try
		end tell
		
	end tell
end open

Once again, thanks for your assistance.

Cheers,
Jon

as mentioned above, choose file returns a single item and and the Finder selection returns a list

You could write


on run
	open (choose file with prompt "Please select the files to link:" with multiple selections allowed)
end run

or if you don’t like multiple selections allowed


on run
	set LinkedFile to choose file with prompt "Please select the file to link:"
	open {LinkedFile} -- makes a list
end run

the part to create the links accepts both Finder and alias specifier

Stefan,

Thank you!

It works!

And somehow the problem I was having with slashes being added between every letter in the link is gone as well.

Now onto getting Word to let me paste this as a hyperlink!

I love learning about this stuff. I think I understand where I was having problems, but if you could take a minute to clarify why it works, I’d appreciate it. My understanding would be that I was trying to use a string where AS was expecting a list, but is that correct?

Thanks again.

Cheers,
Jon

Hi,

the main handler is the on open handler.
From Apple’s documentation:

The open handler takes a single parameter which provides a list of all the items to be opened. Each item in the list is an alias object.

So when you’re going to call the handler explicitly you have to pass a list.
The required alias objects don’t matter in this case because later in the script they are coerced to text
and Finder object specifiers can be coerced to text as well.