Search code examples
javascriptingterminalapplescriptosx-mountain-lion

Applescript Terminal with OSX 10.8


i wrote my first script for downloading downloads reports via Apple's autoingestion.class. It works fine most of the time with OSX 10.7.5, but under OSX 10.8 it sometimes renames files or puts the text for terminal in the applescript editor itself.

Anyone knows how to solve / improve this?

tell application "Terminal"
activate
delay 1 #give time to activate Terminal

tell application "System Events"
    keystroke "cd " & ingestPath #path to autoingestion.class
    keystroke return
    keystroke "java Autoingestion " & userName & " " & userPW & " " & vendorID & " S D S " & reportDate
    keystroke return
end tell

delay 0.1
set frontWindow to window 1
repeat until busy of frontWindow is false
    delay 1
end repeat
#display dialog "finished"
#quit end tell

Thx for your time guys

Edit1: Thx for the fast answer!! I tried the second part and it gives me an Java error, any ideas? Ill try the delay-thing as soon as i got the other macbook again.

MacBook-Pro:~ USER$ java '/Volumes/STICK/Projekte/App_Statstiken/Apple/sales/Autoingestion' USERNAME PW VENDORID S D S 20130718
Exception in thread "main" java.lang.NoClassDefFoundError: /Volumes/STICK/Projekte/App_Statstiken/Apple/sales/Autoingestion
Caused by: java.lang.ClassNotFoundException: .Volumes.STICK.Projekte.App_Statstiken.Apple.sales.Autoingestion
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Edit2: FYI: Updated to new autoingestion.class which uses a propertie to save userName and userPW.

set exePath to ingestPath 
do script "java -cp " & (exePath & space & "Autoingestion autoingestion.properties" & space & vendorID & " S D S " & reportDate)

gives the error that the autoingestion.properties is missing, altough

keystroke "java Autoingestion " & "autoingestion.properties" & " " & vendorID & " S D S " & reportDate 

is working. I tried to put the path in front of the propertie file, but didnt helped. Any ideas?

Working:

do script "cd " & ingestPath & ";java Autoingestion " & userName & " " & userPW & " " & vendorID & " S D S " & reportDate

Solution

  • Many times with keystrokes, the applescript code runs faster than the computer interface can perform the typing... so you have issues. The solution is to put short delays between the typing commands to give the computer interface time to perform the typing. Also your System Events code should not be inside the "tell application Terminal" block of code.

    Try this. You can play with the normalDelay and shortDelay times to make them longer or shorter as needed.

    set normalDelay to 1
    set shortDelay to 0.2
    
    tell application "Terminal" to activate
    delay normalDelay --give time to activate Terminal
    
    tell application "System Events"
        keystroke "cd " & ingestPath --path to autoingestion.class
        delay shortDelay
        keystroke return
        delay shortDelay
        keystroke "java Autoingestion " & userName & " " & userPW & " " & vendorID & " S D S " & reportDate
        delay shortDelay
        keystroke return
        delay shortDelay
    end tell
    
    tell application "Terminal"
        set frontWindow to window 1
        repeat until busy of frontWindow is false
            delay normalDelay
        end repeat
    end tell
    
    display dialog "finished"
    

    Note: I haven't tried this but you may be able to simplify your code as follows...

    set exePath to ingestPath & "Autoingestion"
    
    tell application "Terminal"
        activate
        do script "java " & quoted form of exePath & space & userName & space & userPW & space & vendorID & " S D S " & reportDate
    
        set frontWindow to window 1
        repeat until busy of frontWindow is false
            delay 1
        end repeat
    end tell
    
    display dialog "finished"
    

    EDIT: if you're having problems with the path and other errors (as indicated in your comments) then maybe this approach would work. Try this as your do script command. If the keystroke method works then this will act just like that.

    do script "cd " & ingestPath & ";java Autoingestion " & userName & " " & userPW & " " & vendorID & " S D S " & reportDate