简体   繁体   中英

Java application no longer allowed to talk to iTunes via Applescript

I've changed my Java application to interact with iTunes in a different way but still using applescript, but although it is working for me it seems to be causing problems for alot of my users, one user user has reported this error appearing numerous times

10/20/13 12:37:44.553 PM iTunes[74256]: AppleEvents/sandbox: Returning 
errAEPrivilegeError/-10004 and denying dispatch of event rdwr/writ from process 
'Jaikoz'/0x0-0x413413, pid=19717, because it is not entitled to send an AppleEvent 
to this process.

but I don't understand why he would be getting this error and I am not, any ideas ?

Applescript

tell application "iTunes"
    set thePath to (POSIX file "/tmp/jaikoz_itunes_model.txt")
    set fileref to open for access (thePath) with write permission
    set eof fileref to 0
    set mainLibrary to library playlist 1
    repeat with nexttrack in (get every track of mainLibrary)
        if (class of nexttrack is file track) then
            try
                set trackname to name of nexttrack
                set loc to location of nexttrack
                set locpath to POSIX path of loc
                set persistid to persistent ID of nexttrack
                set nextline to trackname & "::" & locpath & "::" & persistid
                write nextline & "\n" as "utf8" to fileref  starting at eof
            end try
        end if
    end repeat
end tell
return ""

Update I also just realized that I have changed the way I talk to iTunes, I used to use

osascript -a with Runtime.getRuntime().exec()

but now do

 ScriptEngineManager mgr = new ScriptEngineManager();
 ScriptEngine engine = mgr.getEngineByName("AppleScript");

could that be the issue ?

Update 2 This is a pure Applescript problem because a similar error occurs when i run from Applescript editor

25/10/2013 10:39:39.816 iTunes[3366]: AppleEvents/sandbox: Returning 
errAEPrivilegeError /-10004 and denying dispatch of event rdwr/writ
from process 'AppleScript Editor'/0x0-0x24d24d, pid=12717, because
it is not entitled to send an AppleEvent to this process.

The problem is not after all not having access to iTunes, rather iTunes complains every time it writes to the file (or open/closes the file) - why would that be ?

The issue turned out to be the Applescript itself, it had nothing to do with Java or how I was calling Applescript, the error was an error about iTunes not being able to write to files on the filesystem, modifying as follows fixed the problem

set thePath to (POSIX file "/tmp/jaikoz_itunes_model.txt")
set fileref to open for access (thePath) with write permission
set eof fileref to 0
tell application "iTunes"
    set mainLibrary to library playlist 1
    repeat with nexttrack in (get every track of mainLibrary)
        if (class of nexttrack is file track) then
            try
                set trackname to name of nexttrack
                set loc to location of nexttrack
                set locpath to POSIX path of loc
                set persistid to persistent ID of nexttrack
                set nextline to trackname & "::" & locpath & "::" & persistid
                tell current application to write nextline & "\n" as «class utf8» to fileref
            end try
        end if
    end repeat
end tell
close access fileref
return ""

Looks like your difference was:

> set thePath to (POSIX file "/tmp/jaikoz_itunes_model.txt")
> set fileref to open for access (thePath) with write permission
> set eof fileref to 0
2,4d4
<     set thePath to (POSIX file "/tmp/jaikoz_itunes_model.txt")
<     set fileref to open for access (thePath) with write permission
<     set eof fileref to 0
14c14
<                 write nextline & "\n" as "utf8" to fileref  starting at eof
---
>                 tell current application to write nextline & "\n" as «class utf8» to fileref
18a19
> close access file ref

ie the 'tell current application' to do something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM