How can I close a specific Xcode project, from the command line, without Applescript? - xcode

I'd like to close a specific Xcode project from the command line without closing any other projects (so I can't just use kill). Applescript is problematic in Catalina because of additional permissions needed. Is there any other way to do what I want?

If you want to use the Terminal to control other apps via AppleScript, you must give permission. But this is not difficult, because the system will prompt for the needed permission if you have not given it already.
For example, I just said
$ osascript -e 'tell app "Finder" to close window 1'
and I saw this dialog:
I clicked OK and the window closed, as ordered. No problem. So you'd see something like that for Xcode, you'd grant permission, and that would be that. The permission is recorded in the Security & Privacy pref pane under Automation.
If you prefer to pass thru System Events, the permission is recorded under Accessibility. Note that that is a permission you can grant in advance. But you still need to go thru the dialog so you can give Terminal permission to talk to System Events.
If you deny access in the dialog and you want to be asked again, use tccutil to reset the database. Good discussion here: https://apple.stackexchange.com/questions/384230/how-do-i-reset-screen-recording-permission-on-macos-catalina

Related

Running apple script from bash

I have this apple script code that works fine from the Script Editor app:
tell application "Finder" to delete ((POSIX file "/Applications/Symantec Solutions/Symantec Endpoint Protection.app") as alias)
Now, I need to run that from my MDM solution (JAMF), so I'm trying to get it into a bash file as this:
osascript -e "tell application \"Finder\" to delete (POSIX file \"/Applications/Symantec Solutions/Symantec Endpoint Protection.app\") as alias"
but when run it from my editor (CodeRunner) it fails with: execution error: Not authorised to send Apple events to System Events. (-1743)
I think it's related with the Privacy Preferences Policy Control but I cannot make it work.
What I want to accomplish ultimately is that by running a script from my MDM I can get the same dialog that when I drag the app to the bin: "The application “Symantec Endpoint Protection” is hosting system extensions. These extensions will be removed if you continue." which I don't get if I just rm -rf the whole app.
Sorry if I can't be any clearer. Any clues?
Script Editor is (by default) authorized to apple events to other applications. If you want to run an AppleScript that controls other apps from some different context, you have to grant permission to that new context to control other apps. You do this by adding the controlling application to the 'Accessibility' section of System Preferences ➦ Security & Privacy ➦ Privacy.
This gets a bit dicy when you shift contexts a lot. For instance, you seem to be using CodeRunner to execute a shell script to execute OSAScript, which might get a security violation at any level. Try giving permissions to CodeRunner and see if that clears things up.
Thanks a lot Ted, that was the issue, having a lot of contexts chained. I had to tick Finder under CodeRunner in the Automation section of PPPC.
I had a very similar issue with a Python script executing AppleScript in CodeRunner. I just couldn't figure out how to manually add CodeRunner to the Automation section of Security & Privacy in System Preferences.
What ended up working for me, might work for others as well. I had to manually trigger the osascript command within CodeRunner, in order to get the prompt to allow CodeRunner to control Apple Events. Specifically System Events. I did that by creating a new shell script file in CodeRunner and running it.
#!/bin/bash
osascript -e 'tell App "System Events" to display dialog "Testing"'
Upon running that, I was prompted to allow CodeRunner to control System Events. For your particular issue, you'd just want to run the osascript command with AppleScript for Finder instead of System Events
osascript -e 'tell App "Finder to display dialog "Testing This"'
Since you're using an MDM, such as Jamf, you should be able to create a Privacy Preferences Policy Control (PPPC) configuration profile and deploy it to the Mac prior to running the script. You'd just want to ensure that Terminal has access to control Finder or System Events.
The PPPC Utility is a great app for creating those configuration profiles.

How to grant control from applescript over Finder?

I have applications (in JAVA) that send files to Trash.
This is done by osascript; on one account this works, on another it fails.
Obviously the first account lacks permissions to control Finder, which seems to do the hard work. How do I grant this permission in System Preferences, as I do not see any difference between those accounts in the Privacy list.
;JOOP!
it seems that the terminal command
tccutil reset All
is the only way. Of course this is followed by several requests from the system to grant all kinds of privileges, but at least it gives back control to you.
Greetings. ;JOOP!

Reset location permission for app in development

I am building an app in Xcode that uses Core Location. On first run the app asks the user for permission. I want to revoke this permission to trigger the flow anew.
I cannot find my app in the list of apps under Settings / Location Services.
It's odd that your app isn't in Security & Privacy->Location Services. I don't have an answer to that.
If you do uncheck the permission in that pane, though, you won't start the flow anew. Instead you'll be simulating a rejection of permission (user denied the permission request).
The tccutil command line program ought to be able to manipulate those settings. But I tried tccutil reset All on my machine and it didn't affect Location privacy.
It looks like the property list at /var/db/locationd/clients.plist has the authorized applications. So you might be able to edit that file with the plutil command line tool to remove your application. Or just make a copy, edit with Xcode to remove your application, and swap in that copy for testing.
sudo bash
plutil -p /var/db/locationd/clients.plist

Open a shell script in Terminal (Mac) no matter what the default application for bash scripts is set to

I wrote a shell script that I'm distributing to my friends. On my computer, it works great, because I set the default application for shell scripts to be Terminal. However, when my friends open it, the script opens in TextEdit. Is there a way to add an argument to the shell script before it launches so it opens in Terminal instead of a text editor?
Thanks
PS, I did chmod a+x to the file. I also made sure to add #!/bin/sh to the script. I've also been testing with using the extensions .command, .cmd, and .tool. That would solve my problem except then Gatekeeper won't let the script run because I'm not a registered Apple Developer.
If there were a way to make double-clicking a file run it, then that would be the sort of security hole that Gatekeeper is designed to prevent. So, Apple has plugged any such holes they can think of.
Send the file to your friends as a .command file and tell them to right-click or Control-click on it and choose Open. This will change the Gatekeeper dialog to a warning, but with an "Open" button to let them go ahead and open it anyway. The system will prompt them for an administrator password to record the grant of permission in a permanent way. They'll be able to open with a simple double-click from then on.

Terminal set to an illegal value unable to fix

im running a Snow Leopard 10.6.8 and today i tried running Terminal when i got an error which said.
"You are authorised to run this applicationThe Administrator has set your shell to and illegal value."I checked older posts and so far i have tried changing Terminals File Permissions,Changed /private/etc/shells to default and repaired permissions with disk utility. Does anyone know how to fix this.
It sounds like your user shell got set to an illegal value. To fix this:
Open System Preferences, and go to the Users and groups pane
Click the padlock and authenticate as an administrator
Right-click (or Control-click) on your user account in the sidebar, then choose Advanced Options from the shortcut menu that appears.
In the Advanced dialog, change your Login shell to "/bin/bash". DO NOT CHANGE ANYTHING ELSE.
Click OK to save the change, log out & back in, and try using Terminal again.
The default shell for your user will be set in /private/etc/passwd at the end of the line starting with your username. It should be set to /bin/bash

Resources