带有OSX 10.8的Applescript终端

时间:2013-07-29 06:18:10

标签: java scripting terminal applescript osx-mountain-lion

我写了第一个通过Apple的autoingestion.class下载下载报告的脚本。 它在OSX 10.7.5中大部分时间都可以正常工作,但在OSX 10.8下它有时会重命名文件或将终端文本放在applescript编辑器中。

任何人都知道如何解决/改善这个问题?

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

你们这个时代的人们

EDIT1: Thx快速回答!!我尝试了第二部分它给了我一个Java错误,任何想法? 我会在我再次拿到另一台macbook的时候试试延迟事件。

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: 仅供参考:更新为新的autoingestion.class,它使用一个属性来保存userName和userPW。

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

错误 autoingestion.properties缺失

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

正在运作。我试图将路径放在属性文件的前面,但没有帮助。 有什么想法吗?

工作:

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

1 个答案:

答案 0 :(得分:0)

很多次使用击键时,applescript代码运行速度比计算机界面执行打字要快......所以你遇到了问题。解决方案是在键入命令之间设置短暂的延迟,以使计算机接口有时间执行键入。您的系统事件代码也不应位于“tell application Terminal”代码块中。

试试这个。您可以使用normalDelay和shortDelay时间来根据需要更长或更短。

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"

注意 :我没有尝试过,但您可以按照以下方式简化代码......

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"

编辑 :如果您遇到路径问题和其他错误(如评论中所示),那么这种方法可能会有效。尝试将此作为脚本命令。如果按键方法有效,那么这就像那样。

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