Author Topic: Script to check if a file exitst - help!  (Read 2593 times)

0 Members and 1 Guest are viewing this topic.

Offline Erik Youngren

  • Jr. Member
  • **
  • Posts: 52
  • Karma: 1
  • Gender: Male
  • Robotech and Rolemaster freak.
    • Wandering Monk Rolemaster Forums
Script to check if a file exitst - help!
« on: August 23, 2007, 05:09:29 PM »
If anyone can point me in the right direction, I'd be grateful. Not looking for anyone to write me a script, I just need direction.

Right now I have two folder action scripts that process photos in batch through Graphic Converter on an OS9 Mac. Both scripts work absolutely fine even when one is triggered while the other is running.

My problem is that our editor seems to have a difficult time at doing a simple check to see if her photos have arrived in the PROCESSED folder. She either forgets to look or just doesn't feel like it. Now, it's not an issue to have her drop the file back in because once Graphic Converter is done with the batch, it will delete the source file. It will also overwrite the file in the destination again. So that isn't my issue. What becomes the problem is that I get notified that these files have be processed, so I color correct and then save. By this time the editor has decided she isn't going to check again and dumps the file back in. GC does it's thing and my color corrections get wiped out. I have to go back and do this again. I'm getting tired of doing things twice and she seems to ignore my requests to make a check.

SO, I know that you can check that a file/folder exists and then have a script proceed based on that. Our situation is a little different. The source file will be a jpg and the processed file will be a tif with the original filename AND _cmyk.tif or _bw.tif at the end. I have something like this:

property srce_folder : "PHOTO:CMYK_IN:" as alias
property dest_folder : "EDITORIAL:NEWSPAPERS:WEEKLY:Week of 8-30-07 & 8-31-07:GRAPHICS:PROCESSED:" as alias
property err_folder : "PHOTO:ERROR:" as alias

property fString01 : "_cmyk.tif"

on adding folder items to this_folder after receiving these_items
   filecheck(these_items)
end adding folder items to

on filecheck(these_items_)
   tell application "Finder"
      repeat with this_item in these_items
         if not (filename of this_item & fString01 of dest_folder exists) then
            my convertIt(these_items)
              else
                   move it to err_folder
         end if
      end repeat
   end tell
end filecheck

I haven't even run this but I know it's not going to work. How do I tell the script to check for the existence of an already processed file (i.e., a tif of the original jpg)?


Thanks for any assistance....

Erik Youngren
Wandering Monk Rolemaster Forums
17" Powerbook G4 1.67Ghz • 2GB RAM •OSX 10.4.11 • QXP 8.5 • ID CS4 • Suitcase Fusion 2

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Script to check if a file exitst - help!
« Reply #1 on: August 23, 2007, 09:09:10 PM »
Hi Erik,

I believe you are on the right track.

The command for checking the existance of a file under the Finder is:

Quote
tell application "Finder"
    If (not) exists file "DISK:FOLDER:FOLDER:FILE.EXT"
       -- Do your stuff
    end if
end tell

The trick here is to build the "end path" from the original filename. But you have already a procedure for that since your files are generated by script. So you only need to bring your naming scheme back into your FileCheck handler.
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Erik Youngren

  • Jr. Member
  • **
  • Posts: 52
  • Karma: 1
  • Gender: Male
  • Robotech and Rolemaster freak.
    • Wandering Monk Rolemaster Forums
Re: Script to check if a file exitst - help!
« Reply #2 on: August 23, 2007, 11:19:24 PM »
Michel, thanks. I'll take another crack at this when I get back to work in the morning.

Erik Youngren
Wandering Monk Rolemaster Forums
17" Powerbook G4 1.67Ghz • 2GB RAM •OSX 10.4.11 • QXP 8.5 • ID CS4 • Suitcase Fusion 2

Offline Erik Youngren

  • Jr. Member
  • **
  • Posts: 52
  • Karma: 1
  • Gender: Male
  • Robotech and Rolemaster freak.
    • Wandering Monk Rolemaster Forums
Re: Script to check if a file exitst - help!
« Reply #3 on: August 24, 2007, 03:32:18 PM »
Here's what I have now:

property batch_file : "Pueblo13:Desktop Folder:CMYK"
property dest_folder : "EDITORIAL:NEWSPAPERS:WEEKLY:Week of 8-30-07 & 8-31-07:GRAPHICS:PROCESSED" as alias
property err_folder : "PHOTO:ERROR:" as alias

--property fString01 : "_cmyk.tif"

on adding folder items to this_folder after receiving these_items
   filecheck(these_items)
end adding folder items to

on filecheck(these_items_)
   tell application "Finder"
      set fileList to list folder (dest_folder) without invisibles
      repeat with this_item in these_items
         if not (exists this_item in fileList) then
            my convertIt(these_items)
         else
            move it to err_folder
         end if
      end repeat
   end tell
end filecheck

on convertIt(these_items)
   tell application "GraphicConverter Classic"
      repeat with this_item in these_items
         convert file (this_item as file specification) using batch (batch_file as file specification) to folder (dest_folder as file specification)
      end repeat
   end tell
end convertIt

But it's not working. I keep getting an error -2753.

I'm thinking it's the ffileList, but I don't know where to go from here at the moment....

Erik Youngren
Wandering Monk Rolemaster Forums
17" Powerbook G4 1.67Ghz • 2GB RAM •OSX 10.4.11 • QXP 8.5 • ID CS4 • Suitcase Fusion 2

Offline Erik Youngren

  • Jr. Member
  • **
  • Posts: 52
  • Karma: 1
  • Gender: Male
  • Robotech and Rolemaster freak.
    • Wandering Monk Rolemaster Forums
Re: Script to check if a file exitst - help!
« Reply #4 on: August 24, 2007, 03:35:03 PM »
Well, I noticed a typo and fixed that. But now it's an error -1728...

Erik Youngren
Wandering Monk Rolemaster Forums
17" Powerbook G4 1.67Ghz • 2GB RAM •OSX 10.4.11 • QXP 8.5 • ID CS4 • Suitcase Fusion 2

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Script to check if a file exitst - help!
« Reply #5 on: August 24, 2007, 05:03:58 PM »
Hi Erik,

First of all, using Script Debugger would greatly help your task.

Second, once I corrected the typos, I realized that "these_items_" were in fact aliases. Since "list folder" returns file names you cannot ask the finder if any given alias is part of that file names list. Instead, try the following handler:
Quote
001   on filecheck(these_items_)
002      local these_items_, fileList
003      tell application "Finder"
004         repeat with this_item in these_items_
005            if not (exists file ((((dest_folder as text) & (name of this_item)) as text))) then
006               my convertIt(this_item)
007            else
008               move it to err_folder
009            end if
010         end repeat
011      end tell
012   end filecheck
Note:
• The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip all of them.
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Erik Youngren

  • Jr. Member
  • **
  • Posts: 52
  • Karma: 1
  • Gender: Male
  • Robotech and Rolemaster freak.
    • Wandering Monk Rolemaster Forums
Re: Script to check if a file exitst - help!
« Reply #6 on: September 06, 2007, 04:18:30 PM »
Well, I tried several things, but I guess I'm just not getting it. My workaround is to have the original file processed to a processed folder, then copied to a secondary folder than deleted from the source. Clunky and not what I wanted, but at least I don't have files hanging around in the IN folders (GC won't process if it sees a dupe in the processed folder). I suppose I could have just tacked on a delete line after the processing, but this way I get to see what files have been processed. I cobbled the script from pieces I had from others. I'd still like to get this working, but I have spent 2 days out of the last 2 weeks on it so I need to get back to work. Here's what is working:

property source_folder : "PHOTO:CMYK_IN:" as alias
property proc_folder : "PHOTO:CMYK_processed:" as alias
property dest_folder : "EDITORIAL:NEWSPAPERS:WEEKLY:Week of 9-13-07 & 9-14-07:GRAPHICS:PROCESSED" as alias
property error_folder : "PHOTO:ERROR" as alias
property batch_file : "Pueblo13:Desktop Folder:CMYK" as alias

property fString01 : ".jpg"

on adding folder items to this_folder after receiving added_items
   main(source_folder, fString01, 2, proc_folder, error_folder)
end adding folder items to

on main(fda, fnend, lbl, dupfda, errfda)
   tell application "Finder"
      tell (files of item fda whose name ends with fnend)
         set label index to lbl
         update
      end tell
      my convertIt(fda, lbl, dupfda, errfda)
      tell (files of item fda whose label index is lbl)
         set nn to name as list
         my cleanDup(dupfda, errfda, nn) -- delete the duplicate in destination, if any.
         try
            duplicate it to dupfda --with replacing
            deleteFile it with unlocking
         on error the error_message number the error_number
            set the error_text to "Error: " & the error_number & ". " & the error_message
            -- the following line evokes the sub-routine to write the error into an error log created on the desktop
            -- if the file "Script Error Log.txt" already exists, it will add one line to the log
            my write_error_log(the error_text)
            my cleanDup(errfda, nn)
            move it to errfda --with replacing
         end try
      end tell
   end tell
end main

on cleanDup(fda, errfda, nn)
   (*
  alias fda : folder alias to be processed
  list nn : list of item names to be checked
*)
   set dd to {}
   repeat with n in nn
      try
         set end of dd to ("" & fda & n) as alias
      on error -- there's no item with the same name as n in folder fda
      end try
   end repeat
   if dd = {} then return
   tell application "Finder"
      try
         deleteFile dd with unlocking
         --empty trash -- optional
      on error errs number errn
         display dialog "delete failed." & return & errs & " " & errn with icon 2
         error errs number errn
      end try
   end tell
end cleanDup

on convertIt(fda, lbl, dupfda, errfda)
   tell application "Finder"
      set this_item to every file of (files of item fda whose label index is lbl)
      repeat with one_item in this_item
         tell application "GraphicConverter Classic"
            try
               convert file (one_item as file specification) using batch (batch_file as file specification) to folder (dest_folder as file specification)
            on error the error_message number the error_number
               set the error_text to "Error: " & the error_number & ". " & the error_message
               -- the following line evokes the sub-routine to write the error into an error log created on the desktop
               -- if the file "Script Error Log.txt" already exists, it will add one line to the log
               my write_error_log(the error_text)
               move it to errfda --with replacing
            end try
         end tell
      end repeat
   end tell
end convertIt

on write_error_log(this_error)
   set the error_log to ((error_folder) as text) & "Script Error Log.txt"
   try
      open for access file the error_log with write permission
      write (this_error & return) to file the error_log starting at eof
      close access file the error_log
   on error
      try
         close access file the error_log
      end try
   end try
end write_error_log

Erik Youngren
Wandering Monk Rolemaster Forums
17" Powerbook G4 1.67Ghz • 2GB RAM •OSX 10.4.11 • QXP 8.5 • ID CS4 • Suitcase Fusion 2

Offline markuso

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
Re: Script to check if a file exitst - help!
« Reply #7 on: January 10, 2008, 03:26:24 PM »
Hi! 

You've all probably figured this out by now, but when I switched to Tiger File servers, my (if (file/folder myFile/myFolder exists) then) conventions quit working all together.  I pulled my hair out for a while, and ran across an article by Ben Walde where he used the term (if (item myFile/myFolder/myDrive exists) then) and it works every time it's tried.

Just thought I'd share.