Help streamlining a Find and Replace script.

I created an automator File/Folder service to copy the file path of a file/folder to the clipboad. I needed it to be formated in a specific way so that I can send links to fellow co-workers that will take them directly to the file.

Here’s how it was set up:

  1. Automator service that receives files or folders in finder.

  2. Run AppleScript Action:


(* this changes the Applescript path to a POSIX path *)
on run {input, parameters}
	set newPath to (the POSIX path of input)
	get replaceSpaces(" ", "%20", newPath)
	
end run

(* this replaces all spaces with "%20" and adds "file://" prefix so we can paste a clickable link on IM*)
on replaceSpaces(find, replace, subject)
	
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
	
end replaceSpaces


  1. Another Run Applescript Action:

(* this changes the Applescript path to a POSIX path *)
on run {input, parameters}
	
	set newPath to (the POSIX path of input)
	get replaceXserve("xserve", "Volumes/Xsan", newPath)
	
end run


(* this replaces "xserve" to work on the edit stations *)
on replaceXserve(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	set filePrefix to "file://"
	
	return filePrefix & subject
	
end replaceXserve

  1. Copy to Clipboard Action

This give me the path I’m looking for. Something like this:
file:///Volumes/Xsan/Production/Document.doc

Right now everything works but it can be sporadic. I think it might have something to do with running 2 “Run AppleScript” actions. Is there a way to streamline this script?

Thanks!

For step 2 I would refer to my latest post in this topic
For step 3 I would use for HFS to Posix path the posix path of

Step 1 and 4 doesn’t need to change…

foxbot:

This seems like an extraordinary amount of work. Are you just trying to get the path of a file on your local machine for other Users to pick up (and yes, I understand the need for the conformity of the path for IM (though, I take it you’re not using iChat and dragging and dropping files to it?))?

Jim

Hi,

if the path is valid, you can use System Events (or Finder) to get the file URL of the path, for example


set thePath to path to application support folder as text

tell application "System Events"
	set theItem to item thePath
	set theURL to URL of theItem
end tell
--> "file://localhost/Library/Application%20Support/"

Exactly the direction I was going, Stefan.

tell application "Finder" to (set the clipboard to (URL of (selection as alias) as Unicode text))

Grab a file, trigger the script, done. (Unless I’m missing something.) And I think I am, since this returns a localhost reference and the file is remotely hosted. But some quick sed work could do a faster replacement of the localhost with the machine / Volume name.

In some ways this is “six of one, half a dozen of the other” since pulling the posix path is possible too but I would still opt for sed over delimiters to do the character replacements.

Jim

Your script will always throw an error, if two and more files are selected.


tell application "Finder" to (set the clipboard to (URL of (item 1 of (get selection)) as Unicode text))

Yeah, I know. This was more proof of concept one-liner - but thanks. :slight_smile:
Any selections in the Finder I always run through a repeat loop, even if it’s only one file selected, to eliminate this error.

I’m with stefan too, that’s the easiest solution but to make it more waterthight I would use this so you won’t get an error if there is no selection at all (the clipboard will be empty).

tell application "Finder" to set the clipboard to (URL of item 1 of (selection & {{URL:""}}) as Unicode text)

Thanks for all the responses!

We have a huge server with lots of folders within folders. It’s just a lot easier to send direct paths to folks so that they can go straight to what they need. I notice this doesn’t work with AIM but works perfectly through Bonjour (And no we’re not sending the files just the link).

It works when I send a link in this format:
file:///xserve/Production/Documents/Document%20with%20Spaces.txt

It might work with other formats too but I don’t know what it is. The other issue is that some of the workstations are connected through a different file system so their path would look like this:
file:///Volume/Xsan/Production/Documents/Document%20with%20Spaces.txt

So I created 3 actions, one for sending paths to same file systems, one that goes Xserve > Xsan and one Xsan >Xserve. I want to install this on all the workstation so we can send links easily.

Sorry for the long winded explanation. What I have now seems to work but like you guys said I’m pretty sure I’m over doing it. I tried some of what was posted here but I couldn’t get it to work.

when trying something like this:

on run {input, parameters}
	tell application "Finder" to (set the clipboard to (URL of (input as alias) as Unicode text))
end run

it returns:
(
)

I’m very new to scripting so If some one can explain to me more in depth what I need to do I would be grateful. Thanks again!