How Do I Use an AppleScript Variable in a “Do Shell Script” Command (within the AppleScript code)

Can a variable be used in the pdftotext command to specify the page to convert without generating an error?

The following borrowed code works if I enter an actual number (e.g. 3) in the pdftotext command, but generates an error (shown below) using a KM variable.

Execute an AppleScript.kmactions (1.4 KB)

Error:
/var/folders/0q/dkk35gnd3pz4sppnxhphs2qw0000gn/T/Keyboard-Maestro-Script-06632F7C-E321-474A-8F06-5F1FCA30FE8A:630:651: execution error: pdftotext version 3.04
Copyright 1996-2014 Glyph & Cog, LLC
Usage: pdftotext [options] []
-f : first page to convert
-l : last page to convert
-layout : maintain original physical layout
-table : similar to -layout, but optimized for tables
-lineprinter : use strict fixed-pitch/height layout
-raw : keep strings in content stream order
-fixed : assume fixed-pitch (or tabular) text
-linespacing : fixed line spacing for LinePrinter mode
-clip : separate clipped text
-enc : output text encoding name
-eol : output end-of-line convention (unix, dos, or mac)
-nopgbrk : don't insert page breaks between pages
-opw : owner password (for encrypted files)
-upw : user password (for encrypted files)
-q : don't print any messages or errors
-cfg : configuration file to use in place of .xpdfrc
-v : print copyright and version info
-h : print usage information
-help : print usage information
--help : print usage information
-? : print usage information (99)

I'm just an average person here. But I'm looking at your script. Which is the KM variable, Pg_Number? If so, I don't think that's how you use KM variables in an action in KM. The page that describes how to use KM variables is here: https://wiki.keyboardmaestro.com/AppleScript

It explains how to use both local and global variables. Global variables seem to be easier to use, in my opinion. And it looks like you are using globals.

I think the problem is that you are including the AppleScript variable "Pg_nmbr" in the quotes of the shCMD variable.

Try this:

set shCMD to text 2 thru -1 of "
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
pdftotext -f " & Pg_Nmbr & " -l " & Pg_Nmbr & " " & (quoted form of EarningsFile) & " - ;"

@KM_Panther, I hope you don't mind that I have revised your topic title to better reflect the question you have asked.

FROM:
AppleScript Using pdftotext Of A Specific Page Number After OCR

TO:
How Do I use an AppleScript Variable in a Shell Script?

This will greatly help you attract more experienced users to help solve your problem, and will help future readers find your question, and the solution.

JMichaelTX & Sleepy,

JMichaelTX is absolutely right and it works!! Also, your change to the topic title is on point.

Thank you both.

1 Like