I am trying to make a folder action which moves the added pdf-files of this folder (eg. 043123-1 Projectname+something) to another volume wich contains the PDF-folder of that corresponding project (eg. PS6.2/043123 Projectname/043123 PDF files).
The seperate parts of the following code seems to work, but all together it doesn’t.
What am I doing wrong?
set Tvolume to "PS6.2" as alias --Targetvolume
set the Tfolderlist to list folder Tvolume without invisibles
set Tvolume to Tvolume as string
on adding folder items to Sfolder after receiving addedItems
try
tell application "Finder"
set the Sfolder to the name of Sfolder --Source folder.
--Check for SprojectNumber in source folder.
repeat with i from 1 to number of items in the addedItems
set Sitem to item i of the addedItems
set Sitem to (Sfolder & Sitem) as alias
set Sinfo to info for Sitem
set the Sfile to the name of Sinfo
set SprojectNumber to characters 1 thru 6 of Sfile as string
--Compare with corresponding projectnumber on other volume.
repeat with i from 1 to number of items in the Tfolderlist
set Titem to item i of the Tfolderlist
set Titem to (Tvolume & Titem) as alias
set Tinfo to info for Titem
set the Tfolder to the name of Tinfo
set TprojectNumber to characters 1 thru 6 of Tfolder as string
if TprojectNumber is SprojectNumber then
move file Sitem to Titem --& (TprojectNumber & " PDF Proeven"))
end if
end repeat
end repeat
end tell
end try
end adding folder items to
Lets see if you can provide some sample file names (and therefore sample foldernames). If you can parse out the “Project Number”, you can try to find the folder name of the TargetVolume whose name contains the “Project Number”. If there is only one result, then move the file. If not… then you need to solve the logic to resolve how to determine the folder name to move to.
A lot of things seem to be wrong in your script. With a very quick glance, you shouldn’t have code outside of the “on added” handler, it won’t be called when files are added. Next, why are you getting info for an object just to get the name when you are already using the Finder which natively knows the file’s name? You also have a nested repeat with both loops using i as a counter. Don’t do that.
Thanks for this very quick response, mr. Experts.
Me is still a poor scripter, but me is learning.
I am just searching and looking for snippets of code and try to brew something of it. So I really appreciate a bit more explanation.
I don’t think I understand what Digpen means.
In the first part of the script (from --Check for etc)
Sitem is one of the pdf-files
In the second part
Titem is one of the existing projectfolders.
I get the projectnumbers of both with
set SprojectNumber to characters 1 thru 6 of Sfile as string
and
set TprojectNumber to characters 1 thru 6 of Tfolder as string
Now if they’re both the same why does the script not move the file?
Now I have cleaned up the script a little and I think I added Jon suggestions. But it’s still not working…
on adding folder items to Sfolder after receiving addedItems
try
tell application "Finder"
set Tvolume to "PS6.2" as alias --Targetvolume
set the Tfolderlist to list folder Tvolume without invisibles
set Tvolume to Tvolume as string
set the Sfolder to the name of Sfolder --Source folder.
--Check for SprojectNumber in source folder.
repeat with i from 1 to number of items in the addedItems
set Sitem to item i of the addedItems
set the Sfile to the name of Sitem
set SprojectNumber to characters 1 thru 6 of Sfile as string
--Compare with corresponding projectnumber on other volume.
repeat with n from 1 to number of items in the Tfolderlist
set Titem to item n of the Tfolderlist
set the Tfolder to the name of Titem
set TprojectNumber to characters 1 thru 6 of Tfolder as string
if TprojectNumber is SprojectNumber then
move file Sitem to Titem --& (TprojectNumber & " PDF Proeven"))
end if
end repeat
end repeat
end tell
end try
end adding folder items to
It’s a bit difficult to determine exactly what you want but, assuming when you list the folder you get a list of folder names that you start with some code and you want to auto-sort the incoming files into these folders, try this code:
on adding folder items to Sfolder after receiving addedItems
try
tell application "Finder"
set Tvolume to "PS6.2"
set Tfolderlist to list folder (Tvolume as alias) without invisibles --assuming that this is a list of folder names
--Check for SprojectNumber? in source folder.
repeat with i from 1 to (length of addedItems)
set Sitem to item i of addedItems
set SprojectNumber to (text 1 thru 6 of (name of Sitem)) as string
--Compare with corresponding projectnumber on other volume.
repeat with n from 1 to (length of Tfolderlist)
set Titem to item n of Tfolderlist
set TprojectNumber to (text 1 thru 6 of (name of Titem)) as string
if TprojectNumber = SprojectNumber then
move Sitem to folder (Tvolume & ":" & Titem) --& (TprojectNumber & " PDF Proeven"))
exit repeat
end if
end repeat
end repeat
end tell
on error the_error
display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
end try
end adding folder items to
Hmm, you know I got a tip some time ago (not sure from whom) that “length” was preferable to “count” but doing a simple test indicates the opposite:
s
et the_list to {}
repeat with i from 1 to 1000
set end of the_list to 1
end repeat
set start_time_1 to (current date)
repeat 10000 times
get length of the_list
end repeat
set end_time_1 to (current date) - start_time_1
set start_time_2 to (current date)
repeat 10000 times
get count of the_list
end repeat
set end_time_2 to (current date) - start_time_2
return {end_time_1, end_time_2}
--> {5, 0} meaning count is better in terms of time
(Although limiting the repeat to 1,000 times instead of 10,000 results in {0, 0} meaning in most real-world uses, the two are interchangeable with imperceptible performance differences.)
Thanks for your response.
When I use Jon’s code with length of added items I get this:
-Can’t set text 1 thru 6 of name of alias “PS.6.1:etc” (the pdf file in Sfolder) to a string.
count of added item doesn’t do anything at all.
What I exactly want is this:
There is a pdf folder which activates distiller and creates a pdf file. That action is working.
The pdf files are moved by Distiller to folder two, which is the Sfolder in the script.
In Sfolder comes in:
a. a postsriptfile which has to be moved to the trash.
b. a pdf-file which has to be moved to the corresponding projectfolder.
Now the folders on the targetvolume look like this:
043123 Digpen book
043123 PDF Proofs
043143 Corporate ID Jonn
043143 PDF Proofs
043156 Cover Kjeld
043156 PDF Proofs
A PDF-file can be named like: 043143-1 CID-businesscard.pdf
The following code is working, however I can’t get the file into the PDF-folder of the project folder.
Now how to make it work without having to choose for files or folders?
tell application "Finder"
set Tvolume to "PS6.2" as alias --Targetvolume
set the Tfolderlist to list folder Tvolume without invisibles
set Tvolume to Tvolume as string
set Sitem to choose file
set the Sfile to the name of Sitem
set SprojectNumber to characters 1 thru 6 of Sfile as string
--Compare with corresponding projectnumber on other volume.
set Titem to choose folder
set the Tfolder to the name of Titem
set TprojectNumber to characters 1 thru 6 of Tfolder as string
--set TpdfFolder to (TprojectNumber & " PDFproeven")
if TprojectNumber is SprojectNumber then
move file Sitem to Titem --TpdfFolder of
end if
end tell
on adding folder items to Sfolder after receiving addedItems
try
tell application "Finder"
set Tvolume to "PS6.2"
set Tfolderlist to list folder (Tvolume as alias) without invisibles
repeat with i from 1 to (length of addedItems)
set Sitem to item i of addedItems
set SprojectNumber to (text 1 thru 6 of (name of Sitem)) as string
repeat with n from 1 to (length of Tfolderlist)
set Titem to item n of Tfolderlist
set TprojectNumber to (text 1 thru 6 of Titem) as string
if TprojectNumber = SprojectNumber then
move Sitem to folder (Tvolume & ":" & Titem & ":" & TprojectNumber & " PDF Proeven")
exit repeat
end if
end repeat
end repeat
end tell
on error the_error
display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
end try
end adding folder items to
Jon
Unfortunately not. I still get a message like:
Can’t set/convert text 1 thru 6 of name of alias “PS6.1:Users:Kjeld:Desktop:PDF out:043123 Digpenbook.pdf” to a string.
But I haven’t got the time to figure it out more. I will try this evening.
A slight modification and I have now tested that this code works properly (at least in 10.3.2 and I suspect earlier):
on adding folder items to Sfolder after receiving addedItems
try
tell application "Finder"
set Tvolume to "PS6.2"
set Tfolderlist to list folder (Tvolume as alias) without invisibles
repeat with i from 1 to (length of addedItems)
set Sitem to item i of addedItems as alias
set SprojectNumber to (text 1 thru 6 of (get name of Sitem)) as string
repeat with n from 1 to (length of Tfolderlist)
set Titem to item n of Tfolderlist
set TprojectNumber to (text 1 thru 6 of Titem) as string
if TprojectNumber = SprojectNumber then
move Sitem to folder (Tvolume & ":" & Titem & ":" & TprojectNumber & " PDF Proeven")
exit repeat
end if
end repeat
end repeat
end tell
on error the_error
display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
end try
end adding folder items to
This is working perfect! Great. Good to see how you create the path to the subfolders. Never thougth about to add the “:”
I have been playing with this script and it seems to make no difference if I use
length of
count of
or number of items of
All of them give the same result.
I compared it with my earlier scripts and found out I made some essential mistakes in using ‘as alias’ or ‘as string’. However, there is still something strange which I don’t understand. (Food for thought for the experts?). I got back to an earlier version of the script, changed the essential things to make it work and then found out that the script is not working if you activate one of the lines mentioned in the code below.
Now, why is that? Actually it only sets Tinfo or Tfolder to some info out of Titem and has - in my opinion - nothing to do with the rest of the action to be taken. I mean the result of ‘Tprojectnumber’ is always the same, if these lines are there or not.
Anyone?
Many thanks,
Kjeld
on adding folder items to Sfolder after receiving addedItems
try
tell application "Finder"
set Tvolume to "PS6.2" --Targetvolume
set the Tfolderlist to list folder (Tvolume as alias) without invisibles
set Tvolume to Tvolume as string
set the Sfolder to the name of Sfolder --Source folder.
--Check for SprojectNumber in source folder.
repeat with i from 1 to number of items in the addedItems
set Sitem to item i of the addedItems as alias
--set Sitem to (Sfolder & Sitem) as alias
set Sinfo to info for Sitem
set the Sfile to the name of Sitem
set SprojectNumber to characters 1 thru 6 of Sfile as string
--Compare with corresponding projectnumber on other volume.
repeat with n from 1 to number of items in the Tfolderlist
set Titem to item n of the Tfolderlist
--set Titem to (Tvolume & Titem) as string
--set Tinfo to info for Titem --Make this line or the next one active and the script is no longer working.
--set the Tfolder to the name of Tinfo --Make this line or the previous one active and the script is no longer working.
set TprojectNumber to characters 1 thru 6 of Titem as string
if TprojectNumber is SprojectNumber then
beep 2
--move file Sitem to Tvolume --& (TprojectNumber & " PDF Proeven"))
end if
end repeat
end repeat
end tell
end try
end adding folder items to
There are a lot of things wrong with this but the main reason this won’t work is because Sitem is a reference to the complete path to the item so there is no need to add the folder to the beginning of it (it’s already there).
This won’t work because you need to include the “:” between the folders. Once this fails, all the rest of the references to Titem will also fail.
From these lines I know that the script won’t work. (I only deactivated them in an older script to see what was wrong with it; further the script works. Ok. Not such an elegant code…). But I meant thes lines:
--set Tinfo to info for Titem --Make this line or the next one active and the script is no longer working.
--set the Tfolder to the name of Tinfo --Make this line or the previous one active and the script is no longer working.
If you copy the code into script editor the script works (beeps) and when you activate one of these it doesn’t.
won’t work because you need the “:” delimiter. Because this fails, all further references to Titem won’t work. In addition to adding the “:”, to make the next lines work (set Tinfo to info for Titem, etc.), you’d have to change your code to:
set Titem to (Tvolume & ":" & Titem) as string
set Tinfo to info for (Titem as alias)
set the Tfolder to the name of Tinfo
But, again, all of this is moot as you have a streamlined, working solution above.