Page 1 of 1

Apple Script question, change variable

PostPosted: Thu Sep 12, 2013 6:29 pm
by Graf_Wetter
Hi,

I use this script to extract metadata from pdf files

Code: Select all
set posixPath to quoted form of (POSIX path of theFile) as string

set Aufl to (do shell script "exiftool -JoberstellungAuflage " & posixPath & " | awk  -F ' ' '{print$6}'")

return {hazelExportTokens:{Auflage:Aufl}}

The values of "Aufl" are numbers, e.g. "1.000"
Now I need to eliminate the "."
"1.000" should be changed to "1000" before it is written to the custom token.
So in general I need a search&replace for "." within the variable "Aufl" done with Apple Script or "do shell script".
Can anyone help ?
Thanks

Re: Apple Script question, change variable

PostPosted: Sun Sep 15, 2013 6:04 am
by Skeo
I'm rubbish with applescript, but I would use sed to remove a decimal in a shell script.

Code: Select all
sed 's/\.//'

note that the backslash quotes the period so that it isn't confused for a regex.

this syntax finds the first period in the standard input and substitutes it with nothing (i.e. deletes it).
If you want to get rid of all the periods, add "g" to the end.

to feed a variable to the standard input you could pipe it from echo with something like this

Code: Select all
VAR="some code or value"
VAR=$(echo $VAR | sed 's/\.//g')

so if VAR was "1.000", it would then be changed to "1000"
because I added the g at the end of this one. "1.000.000" would change to "1000000". Without the g it would go to "1000.000".

Re: Apple Script question, change variable

PostPosted: Tue Sep 24, 2013 5:59 pm
by Graf_Wetter
Thanks Skeo for your solution.
Works great.
This is my code:
Code: Select all
set posixPath to quoted form of (POSIX path of theFile) as string
set Aufl to (do shell script "exiftool -JoberstellungAuflage " & posixPath & " | awk  -F ' ' '{print$6}'")
set Aufl to (do shell script "echo " & Aufl & " | sed 's/\\.//g'")

Solved.

Graf Wetter