Re: Split PDF and reduce file size - finished script in PDFTK
Re: Split PDF and reduce file size - finished script in PDFTK
- Subject: Re: Split PDF and reduce file size - finished script in PDFTK
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 12 Aug 2005 13:03:52 -0400
Well, that final command *would( work if I'd included the part that
tells pdftk what the heck to do with the file once it finds it. :)
Anyway, the basic rule is simple: always, but always, use "quoted form
of posix path of ..." when passing a file to a UNIX command via "do
shell script".
On 8/12/05, Mark J. Reed <email@hidden> wrote:
> POSIX path and quoting are two separate things. POSIX path just
> gives you the UNIXy version of the pathname - which by definition
> anything you are invoking with "do shell script' requires. Always use
> the POSIX path with do shell script whenever you're passing a
> reference to a file of some sort; no exceptions.
>
> The quoting comes into play because of the way the shell interprets
> commands. AppleScript sends a single string, not a list, so the only
> way the shell can figure out what part of that string is the program
> to run and what part is arguments to pass to the program is by looking
> for space in between. If you pass a command like this:
>
> do shell script "my dog has fleas"
>
> Then the shell will look for a program named "my" and run it with the
> argument list {"dog", "has", "fleas"}. If you wanted to run the
> program "my" with a single argument "dog has fleas", you would be out
> of luck, except that the shell does provide a mechanism for this:
> quoting. There are three ways to quote something in the shell, which
> from the AppleScript point of view are all basically equivalent. A
> backslash will quote the next character, so you can put one in front
> of each space - but remember that backslashes are special to
> AppleScript, too, so you have to double them in your code to get
> single ones through to the shell:
>
> do shell script "my dog\\ has\\ fleas"
>
> Or you can use double quotes, which remove the specialness of all
> spaces between them - but again, because AppleScript strings are also
> enclosed in double-quotes, you have to use backslashes to get them
> through to the shell:
>
> do shell script "my \"dog has fleas\""
>
> Or you can use single quotes (apostrophes), which are the easiest
> because AppleScript cares not a whit about them:
>
> do shell script "my 'dog has fleas' "
>
> That takes care of literal commands, but usually you have variables to
> deal with. So let's walk through an example.
>
> Say you are writing a script to split your PDFs into pages using
> pdftk. First you let the user pick the file:
>
> set pdfFile to (choose file)
>
> Say the user chooses "My Cool PDF File.pdf" on the Desktop. OK, how
> to run pdftk on that? Your first try might look like this:
>
> do shell script "pdftk" & pdfFile -- 1
>
> Which results in this helpful error message:
>
> pdftkMacintosh: command not found
>
> Huh? Oh, duh, forgot the space after the command:
>
>
> do shell script "pdftk " & pdfFile -- 2
>
> Now it says:
>
> pdftk: command not found
>
> Which means that the shell started by AppleScript can't find pdftk -
> this is because it's not installed in a place that the shell looks by
> default. So you have to specify the full POSIX path to where the
> pdftk program lives:
>
> do shell script "/usr/local/bin/pdftk " & pdfFile -- 3
>
> This script takes longer, because it actually finds and runds pdftk,
> but still yields an error, this time issued by pdftk itself rather
> than the shell:
>
> Error: Failed to open PDF file:
> Macintosh
> Error: Failed to open PDF file:
> HD:Users:mreed:Desktop:My
> Error: Failed to open PDF file:
> Cool
> Error: Failed to open PDF file:
> PDF
> Error: Failed to open PDF file:
> File.pdf
> Done. Input errors, so no output created.
>
> There are two different problems here - we didn't use POSIX paths, and
> we didn't quote the spaces. Depending on which one you fix first, you
> get different results. Let's do the POSIX path first:
>
> do shell script "/usr/local/bin/pdftk " & (POSIX path of pdfFile) -- 4a
>
> This yields a similar message:
>
> Error: Failed to open PDF file:
> /Users/mreed/Desktop/My
> Error: Failed to open PDF file:
> Cool
> Error: Failed to open PDF file:
> PDF
> Error: Failed to open PDF file:
> File.pdf
> Done. Input errors, so no output created.
>
> That's because the final command line was something like this:
> "/usr/local/bin/pdftk /Users/mreed/Desktop/My Cool PDF File.pdf".
> Which, because of the space-splitting rule, means that pdftk was run
> with the argument list {"/Users/mreed/Desktop/My", "Cool", "PDF",
> "File.pdf"}, so it looked for files with each of those names and
> couldn't find them.
>
> If, on the other hand, we had quoted the filename without converting
> to POSIX, we'd get a very different error:
>
>
> do shell script "/usr/local/bin/pdftk " & (quoted form of (pdfFile as
> string)) --4b
>
> Error: Failed to open PDF file:
> Macintosh HD:Users:mreed:Desktop:My Cool PDF File.pdf:
> Done. Input errors, so no output created.
>
> In fact, if you didn't know about POSIX paths, this would be a very
> confusing error, since the file exists with that name and it's n ot at
> all obvious why pdftk would fail to open it. The problem is that
> pdftk, and UNIX tools in general, don't understand the Finder version
> of pathnames. So you have to use the POSIX path *and* quote it:
>
> do shell script "/usr/local/bin/pdftk " & (quoted form of POSIX path
> of pdfFile) --5
>
> Which finally works.
>
> --
> Mark J. Reed <email@hidden>
>
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden