Automatic image filing by name

Hi everyone,

I am not sure if this forum is only for applescript or not my questions is more about bash shell in which case my question is rather inappropriate unless someone can think of an elegant solution in Applescript, which would be great.

I am pretty new to scripting and i was wondering if anyone could help me out.

Here is the situation. I am trying to make an action folder that when an image is saved to it, the image is automatically moved to a folder based upon the first 4 or 5 integers of the name.

image names come in a few variations for example:

1000.jpg
10000.jpg
10000-v1.jpg
10000-v1 copy.jpg

any of the first 4 or 5 integers can be from 0-9.

this is the code i came up with so far.


tell application "Finder"

	do shell script " if [ -f ~/Desktop/filing/1[0-9][0-9][0-9][!0-9]*.jpg ]
then
scp ~/Desktop/filing/1[0-9][0-9][0-9][!0-9]*.jpg ~/Desktop/filed/1000/
fi"
	
	do shell script "if [ -f ~/Desktop/filing/1[0-9][0-9][0-9][0-9][!0-9]*.jpg ]
then
scp ~/Desktop/filing/1[0-9][0-9][0-9][0-9][!0-9]*.jpg ~/Desktop/filed/13000/
fi"

end tell

the trouble with this code is the wildcard [!0-9] wont work if the image is just 1000.jpg or 10000.jpg as having the [!0-9] means that it is excluding 0-9 range but expecting a character of some sort.

Anyway if anyone could shed some light on a solution to this that would be great.

Kai

Hi Kai. Welcome to MacScripter.

It’s certainly OK to ask here about things which are accessed from AppleScript, such as a shell script run with the ‘do shell script’ command. :slight_smile:

I’m not a shell expert, but the following forms seem to work for the actual copying. They don’t return true in ‘if’ conditions, though.

It’s not necessary to tell the Finder to run a shell script.


do shell script "scp ~/Desktop/filing/1[0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/1000/"

do shell script "scp ~/Desktop/filing/1[0-9][0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/13000/"

Thank you for such a friendly welcome Nigel and thanks for the code.

The trouble is the image may or may not exist so that code wouldnt really work in this case :frowning:

is there a way i can get it to ignore the error and carry on with the script?

I think bopping " || true" on the end may do it:


do shell script "scp ~/Desktop/filing/1[0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/1000/ || true"

do shell script "scp ~/Desktop/filing/1[0-9][0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/13000/ || true"

Edit: Or all in one shell script:

do shell script "scp ~/Desktop/filing/1[0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/1000/ || true ;
scp ~/Desktop/filing/1[0-9][0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/13000/ || true"

Thanks again Nigel that worked i just tried this aswell that does the same i guess. :smiley:

OK! :slight_smile:

I’d just added an “all-in-one-shell-script” version to my previous post. It also seems to work in this form:


do shell script "scp ~/Desktop/filing/1[0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/1000/ ;
scp ~/Desktop/filing/1[0-9][0-9][0-9][0-9]{,[!0-9]*}.jpg ~/Desktop/filed/13000/ ;
true"

That is much better thanks! :slight_smile:

Hi. I was playing around with a variation of this with another grep pattern, as a learning experiment. My grep knowledge is limited”especially with how it’s integrated in AS. Can anyone more proficient in regex see what’s wrong here? Thanks.

–this regex works in TextWrangler
–\d*(?=[.|-]) --any length number string preceding a period or dash

set grePatt to "\\d{4}(?=[.|-])" --4 number string succeeded by a period or dash
do shell script "ls" & space & "~/Desktop/filing/" & " | grep -E " & grePatt's quoted form

→ error “grep: repetition-operator operand invalid” number 2

Hello Marc.

I am dead sure it is the \d operator, that doesn’t exist in grep regular expressions. You have basically two set of regular expressions you can use with grep, basic, and extended, if memory serves right, you’d activate the extended with the -E switch. For a complete overview of the possibilities, se man re_format.
In grep, you would use the character class [[:digit:]] as a replacement for \d, character classes are included in the basic regular expressions, so you don’t need to activate extended regular expressions for that purpose.

You don’t have the (?=) construct either.) I have edited the post now, so the grep regexp (basic) would look something like this: ( using the extended one, to be able to use the + for one or more occurences, and grouping “()” )

grep -E ".*[[:digit:]]+(.|-)^"

I hope this helps.

Hi,

I have this basic script that works:

set t to quoted form of "a 9090.
b
c
c
c
d
x 1234-
y 5678.
z"
set cmd to "echo " & t & " | grep '[0-9][0-9]*[.-]'"
do shell script cmd

But when I replace with your grep I get an error:

set t to quoted form of "a 9090.
b
c
c
c
d
x 1234-
y 5678.
z"
set cmd to "echo " & t & " | grep -E '.*[[:digit:]]+(.|-)^'"
do shell script cmd

Edited: the error was ‘error “The command exited with a non-zero status.” number 1’

Edited: here’s one for 4 or more digits followed by a “.” or “-”:

set t to quoted form of "a 9090.
b
c
c 54321. zz
c
d
x 1234-
y 5678.
z 123-"
set cmd to "echo " & t & " | grep '[0-9]\\{4,\\}[.-]'"
do shell script cmd

Thanks, McUser, for the man reference, and thank you, kel for the alpha testing. I settled on this form, as I marginally prefer the extended version’s syntax.

do shell script "ls" & space & "~/Desktop/filing/" & space & "| grep -E" & space & "\\<\\d{4}(\\.|-)"'s quoted form

–edited to make pattern begin at the fore

Finally figured out why the egrep script didn’t work. The start anchor (^) was used instead of the end anchor ($):

set t to quoted form of "a 9090.
b
c
c432-x
c
d
x 1234-
y 5678.
z54321.y"
set cmd to "echo " & t & " | grep -E '.*[[:digit:]]+(.|-)$'"
do shell script cmd

I see you use the synonym \d for [[:digit:]]. Out of curiousity, I started to look for the documentation of synonyms, and the only reference I found was a reference to the \w and \W in the grep manual page. But since \w \d and \b appears to work, I guess that all synonyms that are used by PCRE, and perl regexp’s for character classes, are also legal with grep at least, and maybe sed, and the other programs that uses the regexp library. :slight_smile: