Im trying to use a grep command in terminal via shell script. Im getting a error and not sure how to get past it.
set pagecount to do shell script "grep '%%Pages'" & thefile
The problem is too fold, I get a “The command exited with a non-zero status” and I also get a error when there are spaces in the file name or foldername of the path.
You could have 2 problems. 1) to handle spaces in file paths and other shell commands use “the quoted form of” to place single quoted marks around them so the shell will interpret them as one variable. 2) is your “thefile” path a slash delimited path or a colon delimited path? In applescript path components are separated by colons. The shell needs slashes. So make sure thefile is slash delimited. Applescript has a command called “POSIX path of” which will convert the colons to slashes. As such I would write your script like this…
set grepTerms to "%%Pages"
set pagecount to do shell script "grep " & quoted form of grepTerms & space & quoted form of POSIX path of thefile
Hi,
i guess you don’t want to read a complete postscriptfile to search for a special pattern as the filesize could be enormous. You may try to use “Tail” which reads the last part of a file. Have a look at the manpage to play around with the options …
Note: I’ve just tested this with two PS-files and it worked, but I don’t know if any version of Postscript will list the number of Pages as you expected. May be the filereader “less” would be another possibility …
This should be pretty fast:
set ThePSFile to quoted form of POSIX path of (choose file)
set ReadPS to do shell script "tail -b " & "2" & space & ThePSFile --you could use grep here, but AS waits for work too ;-)
set {TID, text item delimiters} to {text item delimiters, "%%Pages: "}
set PageNbr to (text 1 of (text item 2 of ReadPS))
display alert "Number of Pages: " & PageNbr
set AppleScript's text item delimiters to TID
Hope this will be of some help …
thanks Hanz, works great.
Hi,
thx, but it only counts up to number nine ;-).
You could use two steps with AS-Delimiters … or some grep instead …
in this version using the grep-function of satimage osax (have to say untested):
set ThePSFile to (do shell script "tail -b " & "2" & space & quoted form of POSIX path of (choose file))
set ReadPS to find text "(%%Pages: )([0-9]+)" in ThePSFile using "\\2" with regexp and string result --one or several numbers following the string "%%Pages: "
Have a nice day