Counting up pages using Variable Calculations

I want to access incremental pages of search results where the last part of the url is the page number such as
http://www.xxx.com/results/1, http://www.xxx.com/results/2, http://www.xxx.com/results/3, etc.

How do I make a Repeat loop where I open a New Google Chrome Window with URL baseurl/pagenum where pagenum is incrementing?

For example, if I set variable to string baseurl, how do I append the pagenum to get a new calculated URL to use in New Google Chrome Window with URL?

Hi Andrew

I am not sure I completely understand you question.
But the writer Matt Gemmell has made some macros, which sounds like they cover this.

You can find them here:

Look in the Safari section.

Hey Andrew,

This task at its most basic is quite easy.

You have to get the url of the front document in Safari which can be done via AppleScript or a KM-Action.

You then can use a regular expression to find the last available group of digits

Things get a bit fun when you have encoding and/or zero-padding.

Here’s is the AppleScript I’ve been using for many years.

It requires the Satimage.osax AppleScript Extension to be installed for regular expression support and other functions.

As you can see I have a couple of special-cases in there.

All of this can be done with KM alone or by using JXA, but I’m not going to get into that – because I already have a working solution.

-Chris


-----------------------------------------------------------------
# REQUIRES THE SATIMAGE.OSAX
-----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2010/12/08 20:58
# dMod: 2015/06/14 22:55
# Appl: Safari
# Task: Iterate Displayed Image or Page
# Osax: Satimage.osax { http://tinyurl.com/dc3soh }
# Tags: @Applescript, @Safari, @Iterate, @Number
-----------------------------------------------------------------

try
  
  set numPad to "000000"
  
  tell application "Safari"
    tell front document
      set _url to URL
      
      if fndBool("^http://dougscripts\\.com/.*scripts.*\\.php$", _url, 0, 1) of me = true then
        --» SPECIAL-CASE ; DOUG'S SCRIPTS
        set _url to _url & "?page=2"
        set URL to _url
        
      else if fndBool("^https?://www.macupdate.com/?$", _url, 0, 1) of me = true then
        --» SPECIAL-CASE ; MACSCRIPTER
        set URL to "https://www.macupdate.com/page/1"
        
      else
        --» NORMAL ITERATION
        set _url to unescapeURL _url
        set lastNum to fndUsing("(\\d+)[^\\d]*$", "\\1", _url, 0, 1) of me
        
        --» IF NORMAL ITERATION FAILS ATTEMPT TO DISCOVER A 'NEXT' LINK
        --  UNDER CONSTRUCTION --» •••••
        if lastNum = false then
          set pgSrc to source
          set nextLink to fndUsing("(.+\\bnext\\b.+)", "\\1", pgSrc, 1, 1) of me
          if length of nextLink = 1 then
            set nextLink to join nextLink using linefeed
            set nextLink to fndUsing("href=.(https?://.+?)['\"]", "\\1", nextLink, 0, 1) of me
            set URL to nextLink
          else
            error "Too many entries for 'next' found!"
          end if
          
        else
          --» PROCEED WITH NORMAL ITERATION
          set numLength to length of lastNum
          set numPad to text -numLength thru -1 of numPad
          set newNum to lastNum + 1
          set newNum to format newNum into numPad
          set newURL to cng("\\d+([^\\d]*)$", newNum & "\\1", _url) of me
          set URL to newURL
          
        end if
        
      end if
      
    end tell
  end tell
  
on error e number n
  stdErr(e, n, true, true) of me
end try

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
  set e to e & return & return & "Num: " & n
  if beepFlag = true then
    beep
  end if
  if ddFlag = true then
    tell me
      set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
    end tell
    if button returned of dDlg = "Copy" then set the clipboard to e
  else
    return e
  end if
end stdErr
------------------------------------------------------------
on cng(_find, _replace, _data)
  change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
  try
    find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
    return true
  on error
    return false
  end try
end fndBool
------------------------------------------------------------
on fndUsing(_find, _capture, _data, _all, strRslt)
  try
    set findResult to find text _find in _data using _capture all occurrences _all ¬
      string result strRslt with regexp without case sensitive
  on error
    false
  end try
end fndUsing
------------------------------------------------------------

Hey Andrew,

I missed that you were using Chrome.

Change:

tell application "Safari"
     tell front document

To:

tell application "Google Chrome"
     tell front window's active tab

And the script will work in Chrome.

BTW:  I looked at Matt’s Safari macros (that Jimmy pointed out), and it looks like they’ll work fine once adapted to Chrome.

-Chris

Perfect, thanks!

Thanks Chris!