|
|
|
|
#1 |
|
Prospect
Join Date: Jan 2002
Location: Brooklyn, New York
Posts: 6
|
First time scripting, ever. . . Help with Automator and Sed
So, please go easy on me.
I'm trying to write an automator action to resize a postscript file. So far it is set up in automator as follow: It's working well enough, except for the "$1" variable in sed. Instead of getting the value of the variable, the literal $1 is being inserted into the postscript file. I know it's probably something obvious that I'd know if I had any clue what I was doing. . . But, I'm trying. Any help would be appreciated. Thanks, Chris |
|
|
|
|
|
#2 |
|
League Commissioner
Join Date: Sep 2003
Location: Tokyo
Posts: 6,045
|
The single quotes are doing you in - anything inside single quotes doe not get expanded.
And I don't know if the $1 will get replaced by Automator - do you have documentation about it? Any time I've tried to do this kind of stuff I find it easier to build the command with AppleScript and then simply run the command - makes it easier to see what the shell sees. Yes, escaping things like quotes is a major headache. Is the sed expression correct? Replacing "1" seems a bit vague. |
|
|
|
|
|
#3 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,009
|
You got the value of variable PS_File twice. It's clear that you meant to get Reduction_Percent and then PS_File (thus passing Reduction_Percent as $1, and PS_File as $2).
For the command, try: sed -i.backup "s/1 -1 scale/0.$1 -0.$1 scale" "$2 The double-quotes allow the variable $1 to be interpolated, but still prevent the spaces from breaking that argument into separate arguments to sed. It's unreliable to convert a reduction percentage to a value by prepending "0.". What if the percentage is 7? (You'd wind up with 0.7 instead of the expected 0.07.) Or what if it's 150? (You'd wind up with 0.150 instead of the expected 1.50.) One way to handle such cases would be: x=$(perl -e "printf 0.01 * $1") sed -i.backup "s/1 -1 scale/$x -$x scale/" "$2" |
|
|
|
|
|
#4 | |||||||||||||||||||
|
Prospect
Join Date: Jan 2002
Location: Brooklyn, New York
Posts: 6
|
Thanks!
That's extremely helpful. I've left automator for the moment and am focusing on AppleScript alone. It's being a bit more cooperative at the moment. So far I've got the following and it works fine: Code:
set inputFile to (choose file with prompt "Select a Postscript file to read:" of type {"ps"})
set p_inputFile to POSIX path of inputFile
set rawReductionValue to ""
try
set rawReductionValue to text returned of (display dialog "Please enter a reduction precentage:" default answer "100")
end try
set reductionValue to rawReductionValue * 0.01
do shell script "sed -i.backup 's/1 -1 scale/" & reductionValue & " -" & reductionValue & " scale/g' '" & p_inputFile & "'"
The following line appears in postscript file:
That string of number appears a few times in the file. I'd love to be able to replace each instance of the numbersnumber with their values multiplied by the reductionValue variable. Thanks, Chris Last edited by czucker; 05-27-2012 at 05:40 PM. |
|||||||||||||||||||
|
|
|
|
|
#5 | |||||||||||||||||||
|
Prospect
Join Date: Jan 2002
Location: Brooklyn, New York
Posts: 6
|
Okay, I've worked out grep searches to find each of the numbers.
They are as follows:
What I don't know is how to make the strings of numbers that grep returns into AppleScript variables, how to use the p_inputFile variable from above in place of pstest.ps, or how to then have sed change each of the three things I need changed. I've seen that this can be done with sed -e, but I'm using the -i switch for file backup purposes. Last edited by czucker; 05-27-2012 at 08:22 PM. |
|||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|