Applescript Error

Discussion in 'Mac Programming' started by ekulmail71, Nov 7, 2012.

  1. macrumors newbie

    Nov 7, 2012
    Hi all, I'm a complete novice to this and I have no background in programming, I work in prepress. I'm trying to set up an hotfolder where our customer services can drop a pdf to have it create a lo/res version for them to email to the customer. On doing some web searching I came across this script

    property gsSettinglst : "/gs -dSAFER -dCompatibilityLevel=1.4 -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=?¿?outfile -dPDFSETTINGS=/?¿?quality -dPatternImagemask=true -c .setpdfwrite -f ?¿?infile"
    property standardGSPaths : "/usr/local/bin" -- standard paths for gs

    property qualities : {"prepress", "printer", "screen", "ebook", "default"}
    property folder_names : {"prepress", "printer", "screen", "ebook", "default"}
    property magic_bytes : {"%PDF-", "%!PS-Adobe", "≈–”∆"}

    property out_folder : "PDFs"

    property use_quality : 1

    on adding folder items to theFolder after receiving theFiles
    main_action(theFolder, theFiles)
    end adding folder items to

    on run
    -- mainly for debugging
    set f to choose file
    tell application "System Events"
    set theFolder to (path of (container of f))
    end tell
    set theFolder to alias theFolder
    main_action(theFolder, {f})
    end run

    on main_action(theFolder, theFiles)
    tell application "System Events"
    if not (exists folder out_folder of theFolder) then
    make new folder at end of theFolder with properties {name:eek:ut_folder}
    end if
    set f_name to name of theFolder
    end tell
    set out_path to (POSIX path of theFolder) & out_folder & "/"
    --set pathVar to addtoPATH(standardGSPaths)
    --set PATHcall to "PATH=" & quoted form of pathVar & ";"

    set use_quality to find_in_list(f_name, folder_names)

    --display dialog (theFiles as text)
    repeat with X from 1 to count theFiles
    set curr to item X of theFiles
    set file_name to clean_name(curr)

    if check_magic(curr) then
    set thePath to POSIX path of curr
    (POSIX path of theFolder)
    set thePath2 to out_path & file_name & ".pdf"
    tell application "System Events"
    (POSIX file thePath2) as alias
    set thePath2 to out_path & file_name & " " & my date_string() & ".pdf"
    end try
    end tell
    set thePath to quoted form of thePath
    set thePath2 to quoted form of thePath2

    --set currCommand to standardGSPaths & gsSettinglst
    set currCommand to gsSettinglst
    set currCommand to textReplace(currCommand, "?¿?infile", thePath)
    set currCommand to textReplace(currCommand, "?¿?outfile", thePath2)
    set currCommand to textReplace(currCommand, "?¿?quality", item use_quality of qualities)
    set currCommand to standardGSPaths & currCommand

    do shell script currCommand
    log currCommand
    tell application "Finder"
    delete curr
    end tell
    on error theError number theNum
    if theNum ≠ -128 then
    display dialog theError
    end if
    end try
    end if
    end repeat
    end main_action


    on find_in_list(the_val, the_list)
    repeat with X from 1 to count the_list
    if item X of the_list = the_val then
    return X
    end if
    end repeat
    return 5
    end find_in_list


    on clean_name(file_ref)
    tell application "System Events"
    set f_name to name of file_ref
    set f_suffix to (name extension of file_ref)
    end tell
    if f_suffix is not "" then
    set suffix_length to (length of f_suffix) + 2
    set f_name to characters 1 thru (suffix_length * -1) of f_name as text
    end if
    return f_name
    end clean_name


    on textReplace(mainText, fromText, toText)
    set oldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {fromText}
    set textList to every text item of mainText
    set AppleScript's text item delimiters to {toText}
    set mainText to textList as text
    set AppleScript's text item delimiters to oldDelims
    return mainText
    end textReplace


    on check_magic(file_ref)
    set check_strg to "xxxxxxxxxx"
    set fh to open for access file_ref
    set check_strg to (read fh from 1 to 12)
    end try
    close fh
    end try
    repeat with X from 1 to count magic_bytes
    if (offset of (item X of magic_bytes) in check_strg) = 1 then
    return true
    end if
    end repeat
    return false
    end check_magic


    on date_string()
    set now to current date
    return (((year of now) as string) & "-" & pad_2((month of now) as integer) ¬
    & "-" & pad_2(day of now) & " - " & pad_2(hours of now) & "-" & pad_2(minutes of now) & "-" & pad_2(seconds of now))
    end date_string


    on pad_2(num)
    set num to num + 100
    return ((characters 2 thru 3 of (num as text)) as text)
    end pad_2

    I then used Hazel to move the pdf into the screen folder from the original folder
    and set the script to run on pdf entering the screen folder, on testing this I get the following error
    2012-11-07 08:34:59.274 hazelworker[694] Processing folder PDF TEST
    2012-11-07 08:35:01.312 hazelworker[694] Done processing folder PDF TEST
    2012-11-07 08:35:16.942 hazelworker[698] Processing folder PDF TEST
    2012-11-07 08:35:18.960 hazelworker[698] 66455_PL_test.PDF: Rule New Rule matched.
    2012-11-07 08:35:19.130 hazelworker[698] [File Event] File moved: 66455_PL_test.PDF moved from folder /Volumes/JobsCurrent/PDF TEST to folder /Volumes/JobsCurrent/PDF TEST/prepress.
    2012-11-07 08:35:19.738 hazelworker[698] [File Event] File copied: 66455_PL_test.PDF copied from folder /Volumes/JobsCurrent/PDF TEST/prepress to folder /Volumes/JobsCurrent/PDF TEST/screen.
    2012-11-07 08:35:19.782 hazelworker[698] [Error] AppleScript failed: Error executing AppleScript /Library/Scripts/Folder Action Scripts/PDF Convert-1.scpt on file /Volumes/JobsCurrent/PDF TEST/screen/66455_PL_test.PDF.
    2012-11-07 08:35:19.782 hazelworker[698] AppleScript error: {
    OSAScriptErrorNumberKey = "-1708";

    I ran the script through applescript editor and it worked with no errors.
    Any help would be greatly appreciated but it may have to be in lay terms sorry


Share This Page