Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript in Automator - Loops and Variables

Hi to everyone!


I'm trying to create an Automation with a Loop action in which workflow there is an Applescript Action too. What I need to do and I couldn't find anywhere on the net is how to make a variable increase in 1 each times it makes a loop. I will try to explain it again, in case it's not clear.


First Action

Applescript

N=1


Second Action

Loop


So each time it loops, the N variable increases 1 number. First loop, N=1, second loop N=2, third loop N=3 and so on.


Any ideas.


Thank you!

AUTOMATOR-OTHER, Mac OS X (10.7.2)

Posted on Jan 4, 2012 7:26 PM

Reply
Question marked as Best reply

Posted on Jan 4, 2012 8:11 PM

there are a couple of different ways of doing this, depending on the specifics, but keep in mind that they are all sort of hacks. Automator's really not designed with that kind of looping in mind. but you can do the following: create a storage variable called V, and use the following actions


Get Value of Variable (V)


Run Applescript

on run {input, parameters}

return input + 1

end run


Set Value of Variable (V)


Loop


the variable V will automatically be initialized to 0, and will increment its way up with each repetition of the loop

8 replies
Question marked as Best reply

Jan 4, 2012 8:11 PM in response to Marchoto

there are a couple of different ways of doing this, depending on the specifics, but keep in mind that they are all sort of hacks. Automator's really not designed with that kind of looping in mind. but you can do the following: create a storage variable called V, and use the following actions


Get Value of Variable (V)


Run Applescript

on run {input, parameters}

return input + 1

end run


Set Value of Variable (V)


Loop


the variable V will automatically be initialized to 0, and will increment its way up with each repetition of the loop

Jan 6, 2012 11:42 AM in response to Marchoto

well, there are two approaches to this. If you want to use automator, don't bother with a loop - use Get Spcified URLs followed by Get Text From Webpage: that will produce a list of the text from each webpage you include, and you can use a Run Applescript action to process the list. If you want to use Applescript, then you can either open the websites in Safari and extract their source (Safari scripting dictionary has a 'source' property for documents), or you'd run curl in unix to get the webpage source. I think the first is probably easier, unless there's something tricky you need to do with the applescript.

Jan 6, 2012 12:00 PM in response to twtwtw

I will try the first way, but I'm not sure it will work cause the applescript I did is for processing one at the time. In addition, I need to process 600 files at a time and I'm having trouble since the RAM is overloading with the Automator. That's why I want to applescript it. The problem is I don't know much of Javascript or Unix, I'm a quick learner but I try to find some examples and adapt them to what I need.


I think that with the exact same script as "Get text from Webpage" 's Automator action I will be able to acomplish what I pretend. Do you think is possible (relatively easy) to imitate that script?

Jan 6, 2012 1:27 PM in response to Marchoto

well, it's easy enough to wrap a one-shot applescript in a repeat loop, but...


the Get Text From Webpage action is not a script - it's a small cocoa plugin for automator. There's no direct way to get webpages from applescript. normally you use applescript to fetch them in Safari and then read the source from there. however, if you want to do it directly you can use the following:


set webpageText to do shell script "curl http://www.weppage.com"


All together it would look something like:


set webList to {"http://www.weppage1.com", "http://www.weppage2.com", "http://www.weppage3.com"}


repeat with thisURL in webList

set webpageText to do shell script "curl " & thisURL


--process webpageText

end repeat

Feb 14, 2016 6:03 AM in response to Marchoto

Hi,

I am looking to do something similar. That is to replace a variable called V (that is in a Word document) with the values of 1 to n, increasing by 1 every loop.

For example, i have the following text:


- V text....

- V text....

- V text....

- V text....

- V text....

I want the V to be replaced with 1, 2, 3, ......

Could anybody help??

Thank in advance.

Applescript in Automator - Loops and Variables

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.