Find/Replace string in Automator action

(reposting into correct forum)
noob here. I’m trying to pass a URL via Automator to a script that deletes part of the path (or replaces it with “”). Searching around the net, I found some examples of scripts that seem to do this, but they won’t run.

Here is the code I have (it gets its input from a previous action in Automator that gets a URL). It is almost exactly what I’ve found in this same site (and I don’t really understand what it does):


on run {input, parameters}

to switchText of t from s to r
   set d to text item delimiters
   set text item delimiters to s
   set t to t's text items
   set text item delimiters to r
   tell t to set t to beginning & ({""} & rest)
   set text item delimiters to d
   t
end switchText
set theText to input as Unicode text
switchText of theText from "javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=" to ""
   return theText as string
end run

So I’m just trying to delete that part of the URL, or replace it with nothing (“”). When I run it I get: “Expected “end” but found “to””

Help?

Bruce’s response:

The code runs, but it doesn’t replace the text correctly. The input is a list of URLs:


(
    javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=/noticias/calendarioActividades/3/31733/P31733.xml&xsl=/tpl/p43f.xsl&base=/tpl/blanco.xslt');,
    javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=/noticias/calendarioActividades/3/31923/P31923.xml&xsl=/tpl/p43f.xsl&base=/tpl/blanco.xslt');,
    javascript:mostrarEvento('/cgi-bin/getProd.aspxml=/noticias/calendarioActividades/1/32021/P32021.xml&xsl=/tpl/p43f.xsl&base=/tpl/blanco.xslt');,
)

After the code runs, I get a (non list?) string:

javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=/noticias/calendarioActividades/3/31733/P31733.xml&xsl=/tpl/p43f.xsl&base=/tpl/blanco.xslt');NEWTEXT/javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=/noticias/calendarioActividades/3/31923/P31923.xml&xsl=/tpl/p43f.xsl&base=/tpl/blanco.xslt');NEWTEXT/javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=/noticias/calendarioActividades/1/32021/P32021.xml&xsl=/tpl/p43f.xsl&base=/tpl/blanco.xslt');

First, it doesn’t replace the portion of the URL. Second, it doesn’t seem to pass this as a list of URLs (which is how the input comes in).

I would like to just get the unique URL portion (the numbers) and eventually parse these pages into a format I can import into iCal/gCal. I’m so lost…help please.
I don’t understand enough about the subroutine to fix it. P

Browser: Safari 523.10
Operating System: Mac OS X (10.5)

I’ll reply to myself. I found this automator action that does SED expressions:
http://automatoractions.com/files/sedregularexpression1.1.html

Thing is, any letter “t” is treated as a tab. I can’t seem to escape it, and the author doens’t have any documentation. I’m assuming it is just using OSX’s built in SED, so can anyone help?

Here is the problem:

input: list of URLs from previous action. The string start with: javascript:mostrarEvento
expression: s/java/test/g
result: es

cute!
It works with a replacement text that doesn’t have the letter “t”. I’m ok with this, but…if I try to search for “javascript”
expression: s/javascript/AAA/g
result: no match

because “javascript” has a “t”, and it is looking for “javascrip”

Anyone can help?

Since it was related to sed, I took a peek at that Automator action. From the description, the tab-related mangling seems to be intended, but it seems to me that the implementation is buggy. The problem is, as you noticed, that it takes the first “t” character of the input “Regular Expression” (which would be better named “sed Command”) and changes it into a tab character before giving it to sed. I looked at the source code, and it seems to try to change “\t” into tab (a common representation for the tab character), but it fails to escape/quote things properly and ends up mangling the first “t” instead. Even if it was fixed to actually change only the first blackslashed “t” character, it should also probably be modified to change all of them instead of just the first one. There are some other bugs and unfortunate ramifications from the implementation of that Automator action, but I will not go into them.

I have never used Automator before, but I took some time to play with it a bit. I came up with this code that can be used in an Run AppleScript action:

on run {input, parameters}
    if class of input is list then
        set newList to {}
        repeat with i in input
            set end of newList to doMySwitch(contents of i)
        end repeat
        newList
    else
        doMySwitch(input)
    end if
end run

to doMySwitch(str)
    switchText of str from "javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=" to "NEWTEXT"
end doMySwitch

to switchText of t from s to r
    set d to text item delimiters
    set text item delimiters to s
    set t to t's text items
    set text item delimiters to r
    tell t to set t to beginning & ({""} & rest)
    set text item delimiters to d
    t
end switchText

If the input is a list, the output will be a list too. I tested it by putting a Get Specified URLs action before it and a New TextEdit Document action after it. Here are some results captured by inserting View Results actions around it: {"javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=1", "http://craigslist.org", "javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=2", "http://www.apple.com", "javascript:mostrarEvento('/cgi-bin/getProd.asp?xml=three"}

{"NEWTEXT1", "http://craigslist.org", "NEWTEXT2", "http://www.apple.com", "NEWTEXTthree"}

Yeah, I guess SED action is borked. Hopefully it will get fixed.

Thanks for the find/replace script. Worked like magic. How do you get to the point where it all just makes sense and looks like english?
:wink:

Hey everybody !
In Automator this script works for me when it’s run in an applescript action

on run {input, parameters}
	if class of input is list then
		set newList to {}
		repeat with i in input
			set end of newList to doMySwitch(contents of i)
		end repeat
		newList
	else
		doMySwitch(input)
	end if
end run

to doMySwitch(str)
	switchText of str from "this" to "that"
end doMySwitch

to switchText of t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to beginning & ({""} & rest)
	set text item delimiters to d
	t
end switchText

But how is it possible to do multiple find-change queries ?
In the script above there is only one query (replace “this” with “that”) and currently I have to chain multiple actions of this applescript in Automator.
I tried with a repeat action before the “to doMySwitch(str)” without effect.
May someone help me achieving this ?
Thanks !