How to tell AppleScript to work only on the files matched by

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi,

I have again a typical AppleScript dummy issue. The following script should work only on the files matched by my Hazel condition. But now, it copies all files contained in the folders which are defined in the CSV file.

Code: Select all
on hazelProcessFile(theFile)
   
   set collectAllFolderPath to "Pic Archiv:Upload PM:Rename PM Test:"
   set pathPairs to paragraphs of (read (alias "Pic Archiv:Upload PM:Workflows:RenameFolders.csv"))
   
   repeat with i from 1 to count of pathPairs
      set currPaths to item i of pathPairs
      set AppleScript's text item delimiters to ";"
      set theSource to text item 1 of currPaths
      set theDest to text item 2 of currPaths
      set logOne to backup alias theSource onto alias theDest level 2
      set logTwo to backup alias theSource onto alias collectAllFolderPath level 2
      set AppleScript's text item delimiters to ""
   end repeat
   
end hazelProcessFile

And here are a few lines out of my CSV file, just as an example:
Code: Select all
Pic Archiv:10 Austria:Zauchensee:;Pic Archiv:Upload PM:Rename CSV:001 Hotels:y-y-AT-AT-VIE-000-y-y-y-y-u:
Pic Archiv:11 Germany:Muenchen:;Pic Archiv:Upload PM:Rename CSV:001 Hotels:y-y-DE-DE-MUC-223-y-y-y-y-u:
Pic Archiv:01 Hotels HR:Delnice:;Pic Archiv:Upload PM:Rename CSV:001 Hotels:y-y-HR-GK-GRK-173-y-y-y-y-u:
Pic Archiv:01 Hotels HR:Brtonigla:;Pic Archiv:Upload PM:Rename CSV:001 Hotels:y-y-HR-IS-PUY-000-y-y-y-y-u:

Does anybody have a clue what and where to tell the AppleScript to work on the matched files only? I suppose it must refer to the variable "theFile".

Thanks,
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

mediteran wrote:I have again a typical AppleScript dummy issue.

That could easily be mine. :)

I do okay with shell scripting but AppleScript still mostly eludes me.
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

It looks like you are trying to move a file to a new directory based on an association database in your CSV file. Is this correct? You need to be much more clear about your general goal here.

Your script does nothing close to what you are asking for.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

@sjk:

I'm not a scripter at all :-(

In fact, it doesn't matter if I use an AppleScript or a shell script. The task should be "easy":

Files matched by Hazel should be copied from their source folder to a certain destination folder. The folder pairs (source/destination) are defined in a CSV file. Additionally, every matched file needs to be copied to a "collect folder" which remains always the same, for all matched files.

Theoretically, this can be done by Hazel itself. But with having about 180 rules then, Hazel would be overcharged. After some 80 such rules, Hazel says "good bye" and can't even be started anymore…

Any idea?
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

a_freyer wrote:It looks like you are trying to move a file to a new directory based on an association database in your CSV file. Is this correct? You need to be much more clear about your general goal here.

Your script does nothing close to what you are asking for.


Yes, you are right what I want to do. But not move but rather copy ;)

As already said in my reply to sjk: Files matched by Hazel should be copied from their source folder to a certain destination folder. The folder pairs (source/destination) are defined in a CSV file. Additionally, every matched file needs to be copied to a "collect folder" which remains always the same, for all matched files.

Here are my Hazel rules:

1st step: Run rules on folder contents.
Image

2nd step: Match files added today or last edited today, then run the AppleScript which copies the matched files to the destination folders.
Image

Then, Hazel should work on the parent folder of the destination folders:

3rd step: Run rules on folder contents.
Image

4th step: Run an Automator workflow, rename the files, and move them to their last destination
Image

Don't worry about resizing of the image files to 16 pix. I don't need the image data there, but just the correct filename which will be used by a server script to generate a CSV file for database purposes.

I hope I could make a bit clearer ;)
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     <however you match your files>

Do the following to the matched file or folder:
     run shell script (embedded script)
     copy to Collect Folder


The embedded script:

Code: Select all
search_directory=$(basename "$(dirname "$1")")
destination_directory=$(cat "Pic Archiv/Upload PM/Workflows/RenameFolders.csv" | grep "$search_directory" | awk -F ';' '{print $2}' | sed 's/:/\//g')

cp "$1" "$destination_directory/$(basename "$1")"


I just wrote this up on my phone, I have not tested. I'll explain so that you can test in Terminal if you prefer:

search_directory=$(basename "$(dirname "$1")") gets the basename of the directory of the file that you pass to Hazel. (e.g. Zauchensee)


destination_directory=$(cat "Pic Archiv/Upload PM/Workflows/RenameFolders.csv" | grep "$search_directory" | awk -F ';' '{print $2}' | sed 's/:/\//g')

searches your CSV for the second column of the semi-colon delimited CSV file that matches the search directory. It then replaces colons with tabs (posix paths, etc.).

cp "$1" "$destination_directory/$(basename "$1")" Copies the file to the appropriate directory.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Thanks, I did everything exactly as described.

With moving (mv), the files disappear not only from the source folder, but they never arrive at the destination folder (defined in the CSV file). They simply disappear.

The paths of the source and destination folders in the CSV file are definitely correct, I checked them a hundred times now.

With copying (cp), the files don't disappear from the source folder, of course. But again, nothing arrives at the destination folders.

Even Spotlight can't find them anymore.

Is there maybe anything to do with the syntax in the CSV file? I can easily adapt it to the demanded needs.
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

There is probably an error with the script. I'm guessing it's that your paths are not complete.

Check to be sure that your paths are full paths. You may have to prepend (see: extra path):

Code: Select all
search_directory=$(basename "$(dirname "$1")")
destination_directory=$(cat "/Extra/Path/Pic Archiv/Upload PM/Workflows/RenameFolders.csv" | grep "$search_directory" | awk -F ';' '{print $2}' | sed 's/:/\//g')

cp "$1" "/More/Extra/Path/$destination_directory/$(basename "$1")"


Else, do run in terminal. Save the script as an *.sh file, and run it like this:

sh /path/to/the/script.sh "/file/hazel/would/match.whatever"

And you'll see whatever error is tripping Hazel up.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

You are right, the paths haven't been complete. I missed "/Volumes" at the beginning (obviously, AppleScript doesn't need this). I tested it in the Terminal:

With copy (cp) in the script I get this:

Code: Select all
cp: /Volumes/Pic\ Archiv/Upload\ PM/Testdateien/456/Atrium\ 101.JPG: No such file or directory

With move (mv) in the script I get this:

Code: Select all
mv: rename /Volumes/Pic\ Archiv/Upload\ PM/Testdateien/456/Atrium\ 101.JPG to /Volumes/Pic Archiv/Upload PM/Rename CSV Test/Testbilder/def/Atrium\ 101.JPG: No such file or directory

I'm confused…
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Are those paths correct?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

As per Terminal (drag & drop), the paths are correct:

Code: Select all
/Volumes/Pic Archiv/Upload PM/Testdateien/456/Atrium 101.JPG: Permission denied
/Volumes/Pic Archiv/Upload PM/Rename CSV Test/Testbilder/def: is a directory
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

Latest news: After dreaming about the shell script tonight, I just tried something:

Instead of
Code: Select all
sh /Volumes/Ralf\ System/Users/rhobirk/Desktop/testscript.sh "/Volumes/Pic\ Archiv/Upload\ PM/Testdateien/123/Adriatic\ DBV\ 202.JPG"

I put the path into Terminal as follows:
Code: Select all
sh /Volumes/Ralf\ System/Users/rhobirk/Desktop/testscript.sh /Volumes/Pic\ Archiv/Upload\ PM/Testdateien/123/Adriatic\ DBV\ 202.JPG

i.e. without double quotes.

The first 3 tests worked! Will try later more intensively, and with Hazel, of course.
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm

This is pretty basic Terminal usage. I'm sorry that I was not clear and presumed that knowledge. You're escaping the path and indicating special characters with a slash preceding the space. You do one or the other. Not both.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

After tests with a couple of hundreds of files, I can just say THANK YOU!
Best regards from Croatia ;-)
Ralf
mediteran
 
Posts: 37
Joined: Sun Dec 25, 2011 1:08 pm


Return to Support