Getting a Number Range from 1 to X

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
montymay
Posts: 142
Joined: Thu Jul 18, 2013 5:23 am

Getting a Number Range from 1 to X

Post by montymay » Tue Apr 21, 2020 10:47 pm

Hello

After my script gets the page number of the last page of a PDF, I need a script that (1) generates the range of numbers from 1 to the total number of pages and then (2) sets the text of an menu button to those numbers as a list, e.g., if the total pages of an PDF is 3, the script sets the text of an pulldown menu as follows:
Capture.PNG
Capture.PNG (2.4 KiB) Viewed 798 times
The purpose is to enable the user to go to a particular page of a PDF. If I have the range of numbers, I know how to set the text, but am frustrated in finding the script that gets the numbers. I am thinking a repeat loop and an array are used but am asking for confirmation and perhaps a tip if not the actual script. Thanks for your replies.

Monty

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7025
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Getting a Number Range from 1 to X

Post by dunbarx » Tue Apr 21, 2020 11:58 pm

Hi.

Make two buttons. One is an ordinary button, and the other is, say a popup button which you will name "pages". In the script of the ordinary button:

Code: Select all

on mouseUp
   ask "How many pages?"
   if it is an integer then
      repeat with y = 1 to it
         put y into line y of temp
      end repeat
      put temp into btn "pages"
      end if
end mouseUp
Now this needs tweaking, for example, what do you want the popup to "show" as its name before it is loaded? And you will want to modify how the max page number is gleaned from your other handlers. That sort of thing.

Craig

Xero
Posts: 74
Joined: Sat Jun 23, 2018 2:22 pm

Re: Getting a Number Range from 1 to X

Post by Xero » Wed Apr 22, 2020 5:29 am

Gonna take a stab in the dark on this one, as I haven't used the pdf viewer in livecode.
In theory, I guess, you want a dropdown menu that automatically fills with number from 1 to the number of pages in your pdf, which when clicked on, takes you to the page...
First, how are you viewing the pdf? XPDFViewer? or in the revBrowser? I am sure each has their limitations...
What I would do, without exact scripting, is this:
Have the pdf being displayed somehow.
Have a option menu button on screen. Call it "Pages"
When the pdf is opened (loaded into viewer, etc), have a part of the script that opens the pdf that asks how many pages there are in the pdf (In xPDFViewer, it might be something like

Code: Select all

XPDFViewer_Get(viewerName, property)
--i.e.--
XPDFViewer_Get(PDF, pageCount)
Or something similar in revBrowser
Put that number into a script that generates a container that has all of the numbers from 1 to (the number of pages you just found out)
such as:

Code: Select all

put {the number of pages} into tNumberOfPages
if tNumberOfPages is an integer then
repeat with y=1 to  tNumberOfPages
put cr & y after temp
end repeat
delete line 1 of temp --removes leading blank line--
put temp into button "pages"
end if
(much borrowed from Craig's post, but a different way if Craig's way doesn't generate the proper list. I am sure his way will do it, and his is a simpler way!)
All you'll need to do is work out how to get your number of pages, and that may be determined by how you are viewing it...
then, in your menu button "Pages", have a script like this:

Code: Select all

on menuPick theItem
  if theItem is an integer then 
  --script to go to page theItem of pdf. Will be determined by how you're viewing the pdf
  end if
end menuPick
I ran that code through a sample stack that just shifted number from one box to the menu and the menu pick to another box, and it worked fine.
The only thing left is to work through pdf viewing.
Hope that helps. Any more info you can supply will help to fill in the blanks.

montymay
Posts: 142
Joined: Thu Jul 18, 2013 5:23 am

Re: Getting a Number Range from 1 to X

Post by montymay » Wed Apr 22, 2020 6:34 pm

Thank you Craig and Xero

Craig, your script worked. I did not think of the command "put temp into butn" as creating the list of numbers in the button, and I overlooked the use of the chunk line in the repeat loop.

Xero, to answer your question, I am using the PDF browser that comes with the Business License. It is much easier to manipulate than XPDF, and unlike the former two, I don't see how the browser widget allows programmatic navigation to a particular page.

Monty

Klaus
Posts: 12010
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Getting a Number Range from 1 to X

Post by Klaus » Wed Apr 22, 2020 6:42 pm

montymay wrote:
Wed Apr 22, 2020 6:34 pm
... I don't see how the browser widget allows programmatic navigation to a particular page.
It doesn't! Just display the PDF, that's it. :-/

Too bad the PDF widget is only available with the BUSINESS edition.
Should go into the INDY versionb, too!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7025
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Getting a Number Range from 1 to X

Post by dunbarx » Wed Apr 22, 2020 7:14 pm

Monty.

The contents of a button (which is a container just like a field) and in particular a menu-style button, FORMS the menuItems for that button. You can load them by hand from the inspector, of course, or, under script control.

The text property of such a button is the same as the "contents" of that button

One small issue, which I mentioned, is how you choose to use the selection, and to show it or not. So the name of the button is one thing, and the menuItem selection is another. Each of the several styles of menu buttons works a little differently. So experiment.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”