Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 05-25-2012, 11:44 AM   #1
czucker
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
czucker is offline   Reply With Quote
Old 05-25-2012, 05:54 PM   #2
acme.mail.order
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.
acme.mail.order is online now   Reply With Quote
Old 05-26-2012, 06:01 PM   #3
ganbustein
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"
ganbustein is offline   Reply With Quote
Old 05-27-2012, 04:46 PM   #4
czucker
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 & "'"
I would like to add one more thing and I'm not sure exactly how to go about it.

The following line appears in postscript file:
Quote:
2 dict dup /PageSize [612 792] put dup /ImagingBBox null put

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.
czucker is offline   Reply With Quote
Old 05-27-2012, 08:18 PM   #5
czucker
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:
Quote:
Grep to find width:
grep -P -o '(?<=PageSize[[:space:]]\[)[0-9]{3}' pstest.ps

Grep to find height:
grep -P -o '(?<=PageSize[[:space:]]\[[0-9]{3}[[:space:]])[0-9]{3}' pstest.ps

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.
czucker is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 01:13 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.