How to replace a character?

I found a code snippet when searching in the forums, but when trying to implement it myself it won’t work at all!

on replaceText(find, replace, subject)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to prevTIDs
	
	return subject
end replaceText

on adding folder items to dir after receiving theitem
	try
		set directory to dir as string
		set filePlace to theitem as string
		set folderNameLength to length of directory as integer
		set fileName to characters (folderNameLength + 1) thru -1 of filePlace as string
		set theAddress to "http://url.com/" & fileName as string
		set the theAddress to replaceText(" ", "%20", theAdress)
		set the clipboard to theAddress
		
		tell application "GrowlHelperApp"
			set defaultNotification to "Put address on Clipboard"
			set myAllNotesList to {defaultNotification}
			register as application defaultNotification all notifications myAllNotesList default notifications {defaultNotification} icon of application "Finder.app"
			notify with name defaultNotification title defaultNotification description "" & theAdress & " has been put on the clipboard" application name defaultNotification
		end tell
		
	on error
		tell application "GrowlHelperApp"
			set defaultNotification to "Put address on Clipboard"
			set myAllNotesList to {defaultNotification}
			register as application defaultNotification all notifications myAllNotesList default notifications {defaultNotification} icon of application "Finder.app"
			notify with name defaultNotification title defaultNotification description "Something went terribly wrong! :o" application name defaultNotification
		end tell
	end try
	
end adding folder items to

What the code is supposed to do is to take the file name of the file I dropped in a specific folder and then add “http://url.com/filename” to the clipboard. Everything works just fine except separating the filename from its destination. Do you have any suggestions what I’m doing wrong, or how it can be done in some other way?

Thanks!

Hi Kanin,

there are two misprints (theAdress vs. theAddress).
To retrieve the file or folder name is quite easy with the info for command,
try this


on adding folder items to dir after receiving theitem
	try
		set theAddress to "http://url.com/" & name of (info for item 1 of theitem)
		set the clipboard to replaceText(" ", "%20", theAddress)
		growlNotify(theAddress & " has been put on the clipboard")
	on error
		growlNotify("Something went terribly wrong! :o")
	end try
end adding folder items to

on replaceText(find, replace, subject)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to prevTIDs
	
	return subject
end replaceText

on growlNotify(theDescription)
	tell application "GrowlHelperApp"
		set defaultNotification to "Put address on Clipboard"
		set myAllNotesList to {defaultNotification}
		register as application defaultNotification all notifications myAllNotesList default notifications {defaultNotification} icon of application "Finder.app"
		notify with name defaultNotification title defaultNotification description theDescription application name defaultNotification
	end tell
end growlNotify

Wow, that looks much prettier and works beautifully!

Many thanks!!

I think you could also use something like this:

set the clipboard to "http://url.com/" & encodeURL(name of (info for item 1 of theitem))
on encodeURL(someURL)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import quote; " & ¬
		"print quote(unicode(argv[1], \"utf8\"))' " & quoted form of someURL
end encodeURL

. or


set the clipboard to "http://url.com/" & (do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & (name of (info for item 1 of theitem)) & "\")';")

:wink: