jpg to tiff extension change applescript, error checking help needed

Discussion in 'Mac Programming' started by nekslayer, Sep 5, 2008.

  1. macrumors newbie

    Sep 5, 2008
    This is a simple script that just changes the file extension of all the files in the selected folder from .jpg to .tif. and it successfully changes the file extension, the problem is when i run a little error checking if/then statement to make sure the extension is a .tif. i get an error that says "Finder got an error: Can’t get document file" it then shows the file name and path of the current file. if anyone could help me out with this that would be great. and if the syntax isn't correct, feel free to point that out.

    on writeErrors(errorData, logLocation, append_data) -->SubRoutine for Log File
    set the logLocation to the logLocation as text
    set the open_target_file to ¬
    open for access file logLocation with write permission
    if append_data is false then ¬
    set eof of the open_target_file to 0
    write errorData to the open_target_file starting at eof
    close access the open_target_file
    return true
    on error
    close access file logLocation
    end try
    return false
    end try
    end writeErrors

    set this_folder to choose folder with prompt "Choose jpg Folder to convert into tif's"
    set pFiles to {} --problem files
    set cConverted to 0 --count converted
    tell application "Finder"
    set filelist to every file of this_folder
    set tCount to the number of items in filelist as Unicode text
    end tell

    repeat with thisFile in filelist
    with timeout of 43000 seconds
    tell application "Finder"
    if name extension of thisFile is "jpg" then
    set name extension of thisFile to "tif"
    end if
    on error
    set pFiles to (pFiles & {thisFile})
    set label index of thisFile to 2
    end try --if i take out the next 4 lines, the script runs fine
    set nExten to name extension of thisFile --this is where i get the error, "can't get document info"
    if nExten is "tif" then
    set cConverted to (cConverted + 1)
    end if
    end tell
    end timeout
    end repeat

    set errorData to pFiles as text
    set logLocation to (path to desktop as string) & "jpg-tif_errors.txt"
    my writeErrors(errorData, logLocation, true)
    display dialog "All done!" & " Total: " & tCount & " Converted: " & cConverted buttons {"OK"} default button 1 with icon 1 giving up after 10
  2. macrumors 6502

    Jun 16, 2004
    You get an error because you have Finder rename the file, but don't change your variable. So when you go to check if the file is there, it isn't.

    Also, you do realize just changing the extension from jpg to tif doesn't convert the file, right??
  3. thread starter macrumors newbie

    Sep 5, 2008
    sorry for being a noob, this would be part of my first applescript, but I don't understand. I call the variable after i change the file name, and after I end the try statement. I don't rename the file using that variable. And if I have the finder window open, and i throw in a display dialog after i change the file name, but before i call the variable, i can see that the file name has changed.

    Yes i know that it doesn't change the file type, hahaha! The guy before me made duplicates of our photo archival, both jpg and tif. Since $/gig is so cheap now, we are replacing the jpgs with the tifs. Since a tif wont replace a jpg, I'm doing this, and putting it with a larger script that matches the file codes and makes folders by date. I write shell scripts for backup purposes and such, thought i would try applescript for this whole endeavor. cConverted was the first thing that came to mind at that time.
  4. macrumors 65816


    Nov 30, 2003
    I've never played around with AppleScript, but this would be super easy to do using Automator (except perhaps for the file count info, but that's easy to get anyway).
  5. macrumors 65816


    Oct 24, 2007
    Charlotte, NC
    I'm going to assume you still want the file to be a .jpeg and not a true .tif. Changing the extension will not change the actual file type of the image.
  6. thread starter macrumors newbie

    Sep 5, 2008
    i know that it wont change the file type. we have duplicates (jpg, and tif) of every photo in our archive. tif's are on DVD's, jpgs are on data servers. since a .tif file will not replace a .jpg file (with the same file name, pre file extension) i have to replace the .jpg extension with .tif so the file will overwrite it.

    if anyone could help me out with the counter that would be great. the script changes the extension just fine, but it fails once i try to check the file info, after i change the extension, it says it cant get document info?
  7. thread starter macrumors newbie

    Sep 5, 2008
    so i was pissed off that i hadn't gotten any replies, that i found helpful. i ended up just writing out the whole script that this was going into over the weekend. i didn't look at it yesterday cause i was busy. then i looked at the thread tonight, and looked at my original script i posted on here. then i felt like a retard. changed the file, not the variable. sorry for posting this, apparently i didn't know how to read last week. thank you for pointing out the obvious, i was only focusing on the "nExten" variable and my brain didn't work.

Share This Page