Jump to content
Macro Express Forums

Copy part of string from the END


Recommended Posts

Part of one of my macros is dedicated to counting the number of pages in an embedded PDF document. This is accomplished in a chrome browser by issuing a Ctrl+A, saving the clipboard text to a variable %clipboard%, then running a loop that counts down and searches for the text "of 50" "of 49" "of 48" (ie. Page 1 of 48) etc. The macro itself works just fine, but is quite slow. I assume this is because it has to check the contents of a rather large string.

 

I was wondering if there was a way to shorten the string to make the process quicker. The text "Page 1 of XX" always comes at the end of the string but it is impossible to predict how many characters will lead up to it, so I can't use the function to delete or copy part of a string. Is there a smarter way of accomplishing this task? If only MEP allowed you to modify a string starting at the end...

Link to comment
Share on other sites

If I understand the question, if the target "Page x of y" is always at the end, try this:

 

move clipboard data to variable %work%,

set integer %length% to length of %work%,

set %position% to %length% minus 100,

copy %work% from %position% for length of 100 to %shortwork%,

use your existing logic to scan %shortwork% rather than scanning the whole PDF. 

 

100 bytes pulled off the end of the file should be plenty, if the paging is always at the very end.  If not, use 200 bytes, or whatever.  The point is, scanning 100 bytes should be much quicker than scanning the whole PDF. 

 

If you want to speed it up even more, don't search specifically for "of 50", "of 49", etc.  Rather, find the positions of "page " and "of ", then extract the next few characters beyond "of " and that should be the number of pages. 

Link to comment
Share on other sites

You might want to check out A-PDF or similar utilities. I use a few of them with my macros, especially the command line versions. I seem to remember one that would show the properties. Anyway it works great with MEP.

 

I think I would do it a different way. I would open it in Acrobat and get the document properties (ctrl+d). You can use Get Control to get the number of pages. 2020-06-10_16-35-14.jpg.6f27542b7743e452c0de0d533fa59495.jpg 

Link to comment
Share on other sites

Long ago I wrote a backward search macro for a huge bit of text. Basically a nest of conditions. Look at the last letter, next to last, and so forth. Length, length - 1, length -2, etcetera. Then have a nested conditional trap. To find the position of " of " the conditions would be 'if space", "if f", "if o", and "if space". Exit loop there and your loop pointer is the beginning of " of ". Then just do the math. 

Link to comment
Share on other sites

3 hours ago, terrypin said:

So there is no data anywhere directly showing the total number of pages, as there usually is in a regular PDF?

 

Could you show us an example of the PDF? And your macro, or the relevant part of it.

 

1 hour ago, Cory said:

You might want to check out A-PDF or similar utilities. I use a few of them with my macros, especially the command line versions. I seem to remember one that would show the properties. Anyway it works great with MEP.

 

I think I would do it a different way. I would open it in Acrobat and get the document properties (ctrl+d). You can use Get Control to get the number of pages. 2020-06-10_16-35-14.jpg.6f27542b7743e452c0de0d533fa59495.jpg 

 

I think I over-complicated my ask when I mentioned the bit about PDFs. The main point was that I wanted to extract values from a long string, which I do quite frequently in a number of macros that I use, except I was hoping to speed up the process by jumping to the end of the string.

Link to comment
Share on other sites

2 hours ago, rberq said:

If I understand the question, if the target "Page x of y" is always at the end, try this:

 

move clipboard data to variable %work%,

set integer %length% to length of %work%,

set %position% to %length% minus 100,

copy %work% from %position% for length of 100 to %shortwork%,

use your existing logic to scan %shortwork% rather than scanning the whole PDF. 

 

100 bytes pulled off the end of the file should be plenty, if the paging is always at the very end.  If not, use 200 bytes, or whatever.  The point is, scanning 100 bytes should be much quicker than scanning the whole PDF. 

 

If you want to speed it up even more, don't search specifically for "of 50", "of 49", etc.  Rather, find the positions of "page " and "of ", then extract the next few characters beyond "of " and that should be the number of pages. 

 

Thank you, this is what I was hoping to accomplish. I never knew what I would do with the "set integer to the length of a variable"... but now I know and it's a valuable tool that I can add to my arsenal. I will also have to explore what I can do with "Set to position of text in a text variable"

Link to comment
Share on other sites

If it's the total page count you want to extract, check if the same information exists on other screens, like the "Print" dialog box. For Adobe Reader, a Macro Express command might look something like this (in pseudo code):

 

Text Type Control+p  // Print shortcut

Wait 0.5 seconds  // Must wait for the dialog box to appear

Text Type Alt+g  //Jump to "Pages" radio button.

Text Type Tab //Jump to the field containing the page numbers, which is in the form "1 - 10"

Clipboard Copy

Text Type ESC  // Close the Print dialog box

Set String Variable %x% from the Clipboard

Set Integer Variable %position% from position of " - " // Find position of space-hyphen-space

Modify Variable %x% Delete from 1 to %position%

Msgbox "The total number of pages is " %x%

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...