1

So for my work, I am constantly generating links that have to variables that need changed (Ex. http://www.thedomain.com/ajdkeial.html?keyword1={keyword1}&keyword2={keyword2} )

The link is generated with those brackets, to be replaced. Now I am completely illiterate with code, but I am looking for a way to: Select the text, and replace {keyword1} with the actual keyword I want, and {keyword2} with the second.

I envision this being done with clipboard history for the variables to grab via the app "Alfred", however I'm not sure how I could write a script that grabs these variables and replaces the text, then gives me the finished product. If anyone had any direction to go to, it would be greatly appreciated.

1

This is a job for text item delimiters. If you aren't familiar, a delimiter is a separator, normally between words the separator is a space. With text item delimiters, you can change the normal space to any text value you want. In this case, you would make "{keyword1}" the text item delimiters, then the script will see the original text as only two words, everything before "Keyword1" and everything after is the second word. Then you set text item delimiters to whatever you want to replace the "{keyword1}" with, and cram the two words back into one text item.

I've written some basic code that does what you ask below, with some inline comments. One note, whenever playing with text item delimiters, always finish the script by setting them back to the default. I stored the original text item delimiters in a variable called "tid", then set them back at the end of the script.

--Set the variables
set originalText to "http://www.thedomain.com/ajdkeial.html?keyword1={keyword1}&keyword2={keyword2}"
set key1 to "{keyword1}"
set key2 to "{keyword2}"
set subKey1 to "NewKeyWord"
set subKey2 to "AnotherKeyWord"

--Main Script
set tid to text item delimiters
set text item delimiters to key1
set tempList to every text item of originalText
(* the line above returns two item, everything before {keword1} and the second item is everything after {keyword1} *)
set text item delimiters to subKey1
set newText to every item of tempList as text
(* The line above takes the two items from tempList and puts the replacement key word between them *)
--Below, do the same for the second keyword
set text item delimiters to key2
set tempList to every text item of newText
set text item delimiters to subKey2
set newText to every item of tempList as text
set text item delimiters to tid
return newText --> "http://www.thedomain.com/ajdkeial.html?keyword1=NewKeyWord&keyword2=AnotherKeyWord"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .