I try to make a script (folder action or droplet) that move a file to a specific subfolder.
eg. the filename is name.surname.A01B02.jpg or name.middlename.surname.A01B02.jpg or name.A01B02.jpg < This file should be moved to the folder āname surname:film 01:ā or āname:film 01:ā(for the second case) or āname middlename surname:film 01:ā (3rd case)(if the folders do not exist they should be created and than be moved)
name(.middlename.surname) is always different and some files have the .surname part and some not.
My Question now is:
How can I get it to work that the script will make folders and subfolder out of the filename (also if there are 2 different kind of structures in the files) and move these accordingly?
thx for any suggestions and help in advance
goeste
Model: MB mid2007
AppleScript: version from Leo 10.5.5
Browser: Firefox 3.0.3
Operating System: Mac OS X (10.5)
This isnāt complete, but it gets you most of the way there. If you canāt finish it, Iāll try to get to it again tomorrow.
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to (every file in thisfolder whose name is not "thenames.scpt") as alias list
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set n to name of eachfile
set thefirst to text item 1 of n
set themid to text item 2 of n
set thelast to text item -2 of n
if themid = thelast then
set thefoldername to thefirst
else
set thefoldername to thefirst & " " & themid
end if
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
move eachfile to newfolder
end repeat
end tell
cwtnospam: thx this seems almost what i want, but ;):
the script ist fixed to three parts in front of the A01B02-part. What if the front-part has 4,5,6,7,8,9 namens?
What is the āthenames.scptā-script for?
And it always creates a folder as far as i can see it doesnāt look if the folder already exists or am I missing sth?
But still a very nice start but unforunately not exactly what is neded for my workflowā¦
but Iāll sit on it during lunch-time,too, to try to add my ideas maybe we can work it out togetherā¦
kind regards
goeste
EDIT:
To make it more clearly:
a.b.A01B02.jpg sould be moved to folder āa b:film 1:ā and in āa b:film 1:ā does not exist it should be created first and than the file should be moved.
a.b.c.d.e.z.A02B99.jpg sould be moved to folder āa b c d e z:film 2:ā and in āa b c d e z:film 1:ā does not exist it should be created first and than the file should be moved.
and so onā¦ everithing should be done by only one script if possible (as droplet or folder-action)
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to every file in thisfolder as alias list
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set filename to name of eachfile
set thefirst to text item 1 of filename
set themid to text item 2 of filename
set thelast to text item -2 of filename
try
set As_a_number to (text 2 thru 3 of thelast) as number
on error
set As_a_number to 0
end try
if (As_a_number > 0) then
set mysubfolder to "film " & text 2 thru 3 of thelast
if themid = thelast then
set thefoldername to thefirst
else
set namecount to count of text items in filename
set thefoldername to ""
repeat with i from 1 to namecount - 2
set thefoldername to thefoldername & " " & text item i of filename
end repeat
end if
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
try
set thedestination to make new folder at newfolder with properties {name:mysubfolder}
on error
set thedestination to ((newfolder as text) & mysubfolder) as alias
end try
move eachfile to thedestination
end if
end repeat
end tell
It works for me. For example, the first file ends up in a folder called āfilm 01ā inside a folder āUntitled copy 1ā, which is inside the original folder. The filed called āUntitled.txtā doesnāt get moved because it doesnāt have a film number.
okā¦
Is AS counting from the back to the front in the filename (the text items)?
because if i use filenames that do not have anything after the film number it works. e.g.
ātest.test2.test3.test4.S02E06.jpgā will be moved to ātest test2 test3 test4/film 1/filename.jpgā
BUT
āOhne Titel.s01e02.ded.txtā there happens just nothingā¦
Actually, ātest.test2.test3.test4.S02E06.jpgā will be moved to ātest test2 test3 test4/film 02/filename.jpgā because it gets the number 02 from S02E06, and āOhne Titel.s01e02.ded.txtā doesnāt get moved because the script expects a number in the section that you have as ādedā and when it doesnāt find one, it skips the file.
There needs to be a way to be sure where the film number is. I assumed it would always be in the last section before the file extension. If thatās not the case, you need to describe the places it can be found and how they would be different from a file where the number just isnāt there. For example, could a file be named: āmovie.made.D01-10-2008.A01B02.jpgā and another be: āmovie.A01B02.made.D01-10-2008.jpgā ? If so, and if there are other possibilities for names, how can we determine programatically where the number is?
the filmnumber is ALWAYS in the same format āA0XBXXā maybe that can help? but i found one more thing (sry to bomb u that muchā¦) there is always a trailing space " " in front of the first word of the folder. And is there an optin to make every first letter a Capital?
sry again for so much things i ask for. i am just an try and error guy and when it comes to real programing i really suckā¦! but i want to change thisā¦ i am not aware of al the commands AS understands and so onā¦
BACK TO PROBLEMā¦
couldnāt it be done with the
if filename contains AXXBXX then
blablabla
else
blub
end if
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to every file in thisfolder as alias list
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set filmnum to 0
set filename to name of eachfile
set thefirst to text item 1 of filename
set themid to text item 2 of filename
set thelast to text item -2 of filename
set n to number of text items in filename
set thefoldername to ""
repeat with i from 1 to (n - 1)
try
set txt to text item i of filename
set num1 to text 2 thru 3 of txt
set num2 to text 5 thru -1 of txt
set num1 to num1 as number
set num2 to num2 as number
set filmnum to text 1 thru 2 of ("0" & (num1 as text))
on error
set thefoldername to thefoldername & txt & " "
end try
end repeat
if (filmnum > 0) then
set mysubfolder to "film " & filmnum
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
try
set thedestination to make new folder at newfolder with properties {name:mysubfolder}
on error
set thedestination to ((newfolder as text) & mysubfolder) as alias
end try
move eachfile to thedestination
end if
end repeat
end tell
But how would I know that ābla.bla.bla.AXXBYY.mnopqr.n.o.jpgā should go to folder:
āBla Bla Bla:film XX:bla.bla.bla.AXXBYY.mjkpqr.n.o.jpgā
and not:
āBla Bla Bla:film jk:bla.bla.bla.AXXBYY.mjkpqr.n.o.jpgā
without checking for a number??
By the way, I found a small bug.
This line
set filmnum to text 1 thru 2 of (ā0ā & (num1 as text))
should be:
set filmnum to text -2 thru -1 of (ā0ā & (num1 as text))
and this line is no longer needed:
set themid to text item 2 of filename
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to every file in thisfolder as alias list
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set filmnum to 0
set filename to name of eachfile
set thefirst to text item 1 of filename
set thelast to text item -2 of filename
set n to number of text items in filename
set thefoldername to ""
repeat with i from 1 to (n - 1)
try
set txt to text item i of filename
set num1 to text 2 thru 3 of txt
set num2 to text 5 thru -1 of txt
set num1 to num1 as number
set num2 to num2 as number
set filmnum to text -2 thru -1 of ("0" & (num1 as text))
on error
set thefoldername to thefoldername & txt & " "
end try
end repeat
if (filmnum > 0) then
set mysubfolder to "film " & filmnum
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
try
set thedestination to make new folder at newfolder with properties {name:mysubfolder}
on error
set thedestination to ((newfolder as text) & mysubfolder) as alias
end try
move eachfile to thedestination
end if
end repeat
end tell
OK. You are right. But maybe its my fault becaus I didnāt exactly write what I need this forā¦
above are the pictures. A is for the filmnumber and B is the Camera that has taken the photo the part after B (may.berlin.germany) is only info for the file.
all files above should go to the Folder āMy First Holidayā. The first to filse go into a subfolder film 01 (the B-part doesnāt play a role). The last 3 file go to the subfolder film 02 (the bB-part still doesnāt play a role). The additional info is only for others to see where the pictures hav been taken and when.
the final structure of the folder:
My First Holiday
|
+film 01
| -my.first.holiday.A01B01.may.berlin.germany.pic001.jpg
| -my.first.holiday.A01B02.may.berlin.germany.pic001.jpg
+film 02
| -my.first.holiday.A02B01.may.berlin.germany.pic001.jpg
| -my.first.holiday.A02B02.may.berlin.germany.pic001.jpg
| -my.first.holiday.A02B03.may.berlin.germany.pic001.jpg
I -my.first.holiday.A02B03.may.berlin.germany.pic002.jpg
hope to make more clear now. sry for any misunderstandingsā¦
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to every file in thisfolder as alias list
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set filmnum to 0
set filename to name of eachfile
set thefirst to text item 1 of filename
set thelast to text item -2 of filename
set n to number of text items in filename
set thefoldername to ""
repeat with i from 1 to (n - 1)
try
set txt to text item i of filename
set num1 to text 2 thru 3 of txt
set num2 to text 5 thru -1 of txt
set num1 to num1 as number
set num2 to num2 as number
set filmnum to text -2 thru -1 of ("0" & (num1 as text))
on error
if i < 3 then
set thefoldername to thefoldername & txt & " "
else if i = 3 then
set thefoldername to thefoldername & txt
end if
end try
end repeat
if (filmnum > 0) then
set mysubfolder to "film " & filmnum
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
try
set thedestination to make new folder at newfolder with properties {name:mysubfolder}
on error
set thedestination to ((newfolder as text) & mysubfolder) as alias
end try
move eachfile to thedestination
end if
end repeat
end tell
it works for files with 3 items before the filmnumber but not when there are 4 oder more and when there are less then 3 words, the folder always has space at the end.
Isnāt there a possibility to count the text items before the filmnumber? because the part before the filmnumber is always the name of the trip and this changes from 1 to X words. everything else is fine and nearly perfect only the capitalization of every text item in the name (part before the filmnumber)ā¦
tell application "Finder"
set thisfolder to (choose folder) as alias
set mylist to every file in thisfolder
set thisfolder to thisfolder as text
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
repeat with eachfile in mylist
set filmnum to 0
set filename to name of eachfile
set thefirst to text item 1 of filename
set thelast to text item -2 of filename
set n to number of text items in filename
set thefoldername to ""
set z to 1
repeat with i from 1 to (n - 1)
try
set txt to text item i of filename
set num1 to text 2 thru 3 of txt
set num2 to text 5 thru -1 of txt
set num1 to num1 as number
set num2 to num2 as number
set filmnum to text -2 thru -1 of ("0" & (num1 as text))
set z to i - 1
end try
end repeat
if z > 3 then z = 3
repeat with i from 1 to z
set txt to text item i of filename
if i < 3 then
set thefoldername to thefoldername & txt & " "
else if i = 3 then
set thefoldername to thefoldername & txt
end if
end repeat
if (filmnum > 0) then
set mysubfolder to "film " & filmnum
try
set newfolder to thisfolder & thefoldername as alias
on error
set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
end try
try
set thedestination to make new folder at newfolder with properties {name:mysubfolder}
on error
set thedestination to ((newfolder as text) & mysubfolder) as alias
end try
move eachfile to thedestination
end if
end repeat
end tell
O god, what a messā¦
i suggest you, goeste, to explain more in detail your needs, because these are not well to read from your post.
In second place please seach in the forum for snippets, where are potentially of interesting for you, to learn a bit more the applescript language. Third, i think its much more funny to write applescripts by yourself, as to ask a pre-maked script here.
try to adjust the script below to your needs:
--on adding folder items to this_folder after receiving these_items
tell application "Finder"
--set y to ((path to pictures folder) & "MyPortfolio:" as text)
repeat with i from 1 to number of items in y
set this_file to item i of y
set g to (name of y as text)
set f to count characters of (name of y as text)
set h to count words of (y as text)
set j to h - 2
set gg to words 1 thru j of g
set ff to (count characters 1 thru -1 of (gg as text)) + j
set yess to (characters 1 thru ff of (name of this_file as text) as text)
if not (exists folder yess) then
make new folder at front window with properties {name:yess}
end if
set target_f to folder yess of front window
try
set c_fold to count (folders in target_f) + 1
on error
set c_fold to 1
end try
with timeout of 1 second
make new folder at target_f with properties {name:("film" & c_fold as text)}
end timeout
move this_file to target_f
-- insert other actions here
end repeat
end tell
--end adding folder items to
I think that given the fact that most of the time I spent writing the script was spent trying to figure out what somebody on the other side of the planet wanted, it came out pretty well. Sure, it could be more efficient, but it works well enough.
If youāre going to offer an alternative, it really should work.