How to run an AppleScript from a sandboxed application on a Mac (OS X)
Asked Answered
K

3

11

We are developing an application for the Mac App Store using Qt 5.2.0.Framework on MacOSX 10.9.

Here is a simple AppleScript that creates a Microsoft Excel workbook and saves to any location.

tell application "Microsoft Excel"
    set myworkbook to make new workbook
    set fname to POSIX file "/Private/var/root/Download/ExcelFile" as text
    save workbook as myworkbook filename fname
end tell

The above script is saved as Untitled.scpt in /Library/ApplicationScript/.

Inside the application, we use the Cocoa framework to execute the AppleScript.

This AppleScript works in a non-sandboxed application. It fails in a sandboxed application.

My question is: How do you use an AppleScript in a sandboxed application? Or is there an alternative to this?

Kindly tell me the solution, because my project is being delayed by this.

Thanks

enter image description here

enter image description here

Kimono answered 21/2, 2014 at 3:51 Comment(4)
I've reformatted your question to make it more readable; please take a look at the source, so you can perform this formatting yourself in the future; You first say that the script filename is new.scpt, but your Objective-C code references Untitled.scpt. I know little about sandboxing, but I wouldn't expect paths such as /private and /Library to be writable from a sandboxed application. How is the .scpt file saved to the target location?Chandless
This,apple script save as Untitled.scpt (not new.scpt) in this path "/Library/ApplicationScript/Untitled.scpt". when execute the script using cocoa, excel file is created which save as /Private/var/root/Download/ExcelFile this locationKimono
replace keyword "new.scpt" to "Untitled.scpt" in my quectionKimono
This is a good post to read objc.io/issues/14-mac/sandbox-scriptingColored
G
10

There two issues with your code:

  • Excel probably doesn't yet support com.apple.security.scripting-targets, so you would need com.apple.security.temporary-exception.apple-events (see here how to find out if it supports it and here how to work with the temporary exception by adding an array of bundle-identifiers you want to target. You had that in the old screenshot of this question.)

  • The entitlement of scripting-targets as well as for com.apple.security.temporary-exception.apple-events is an array of bundle-identifiers. You would see it in Xcode like this: com.apple.security.temporary-exception.apple-events

  • A Mac App Store app must not install anything in shared locations like /Library/ApplicationScript (see App Store Review Guidelines Section 2.15). You need to store the Script inside your Container and run it from there.

Griffis answered 21/2, 2014 at 16:1 Comment(7)
I am not understand the key value for com.apple.security.temporary-exception.apple-events or com.apple.security.scripting-targets in entitlement file. what is the correct value of this key. which execute the apple script on sand boxed application.Kimono
Thanks mahal for solve the big problem of my project.Kimono
can not execute do shell script command using cocoa. cocoa code dummy is to see above my question. descriptor variable for NSAppleEventDescriptor is return a NULL value. my script file is contain this command "do shell script "diskutil info / | grep \"Volume Nmae:\"" "Kimono
this is a new question. to "do shell script" you would need to have another entitlement. And for diskutil you're better off doing it with Disk Arbitration developer.apple.com/library/mac/documentation/…Griffis
My Apple Script is successfully run on 10.7,10.8.5,10.9.but It fails in a Mountain Loin(10.8 to 10.8.4) . My apple script store in app container. script path is -> "/Users/UserName/Library/Containers/com.comanyName.ProductName/Data/Library/AppleScripts/"Kimono
My project is delay because apple script is not run on 10.8 in sand-boxed application.Kimono
You probably have to work without 10.8 to 10.8.4 as this sandbox-implementation is full of bugs. Check when starting the App for the OS and ask user to update to 10.8.5.Griffis
E
2

You need to add the com.apple.security.scripting-targets sandboxing entitlement to script other apps from within the sandbox.

Eudiometer answered 21/2, 2014 at 5:0 Comment(7)
I am new user on MAC,In entitlements ,i am create a new entry for com.apple.security.scripting-targets . but how to create application-group-id.Kimono
You just add the keys/values from the link above (it has an example) to your .entitlements file.Eudiometer
see the screen shot for entitlements file is attach to this question. Adding the scripting-targets key in sandbox application,but new created sandbox application is not execute the apple script.Kimono
com.apple.security.scripting-targets is not a boolean value, it's an array of strings. Please read the article I linked to.Eudiometer
script is not run after adding a new key com.apple.security.scripting in entitlement file. (see the second screen shot for entitlement file). If key value is wrong ,please tell me correct value and provide me a suitable example which solve the my problem.Kimono
My Apple Script is successfully run on 10.7,10.8.5,10.9.but It fails in a Mountain Loin(10.8 to 10.8.4) . My apple script store in app container. script path is -> "/Users/UserName/Library/Containers/com.comanyName.ProductName/Data/Library/Appl‌​eScripts/"Kimono
other alternative method which run apple script on 10.8 to 10.8.4 in sand-boxed application . but, In nonsand-boxed application my apple script is successfully run on 10.8 , means this is bug for apple script on sand-boxed applicationKimono
F
1

To work with Apple Script from your app you have to add the bundle identifiers of the app you want to send or receive events from in the entitlements file.

Here is the syntax:

<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>{bundle identifier of app 1}</string>
    <string>{bundle identifier of app 2}</string>
</array>

You can have one or multiples app bundle identifiers in that array. Now, these apps can receive your apple events commands.

Example:

<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>com.apple.finder</string>
    <string>com.apple.systemevents</string>
    <string>com.microsoft.excel</string>
</array>
Freud answered 12/11, 2018 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.