Activate App but keep script running Applescript - applet

I would like to open another app (applescript applet) with Applescript but still move on to the next line in my script.
I though it would automatically do this but it does not for some reason.
Here is my code:
...
if selectedMenu is "1" then
set theDisplay to "⬟"
my display(theDisplay)
tell application "/Users/Patrick/Documents/Programming/Applescript/Applications/NoWatch.app/Contents/Resources/Both.app" to activate
tell application "/Users/Patrick/Documents/Programming/Applescript/Applications/NoWatch.app/Contents/Resources/VNC.app" to quit saving no
tell application "/Users/Patrick/Documents/Programming/Applescript/Applications/NoWatch.app/Contents/Resources/SSH.app" to quit saving no
...
I included the part after the activation just incase that is what is causing this.
Also, after closing the app I activated, the script continues like normal.

ignoring application responses
tell application "Finder" to activate
end ignoring

Related

Could not locate login item in the caller's bundle error when using SMLoginItemSetEnabled

I'm trying to get my swift Mac app to launch at login using the method described in this page: https://theswiftdev.com/how-to-launch-a-macos-app-at-login/
However, I keep getting the following errors as soon as I call SMLoginItemSetEnabled:
Could not locate login item com.domain.LauncherApplication in the caller's bundle
Could not enable login item: com.domain.LauncherApplication: 3: No such process
I checked that the launcher app ID is correct multiple times, I tried changing it and changing its version number. I even tried cleaning the project and moving the base app to /Applications but I always get these error messages.
Any idea what the problem might be? (Notice the solution must not require me to disable App Sandboxing)
OK, I found the problem but it took a long time since it was so sneaky: In the Copy File Build Phase section I entered "Contents/Library/LoginItem" as the subpath instead of "Contents/Library/LoginItems" (notice the 's' in the end - can't believe I missed it). So thank you #vadian! You were absolutely right.

Error 1002 in applescript that uses "key code" when called from macOS app

I'm trying to run an AppleScript from my macOS app (Mojave, Xcode 10.3). However, when the AppleScript is run from the app, I get this error:
/Users/my_username/Desktop/test_apple_script.scpt: execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
Based on other posts, I have made sure that my app can control my computer (System Preferences > Security & Privacy > Privacy > Accessibility), and it also has control over System Events (System Preferences > Security & Privacy > Privacy > Automation).
Here is my AppleScript:
tell application "System Events"
key code 48 using {command down}
end tell
The script works when run in Script Editor (I only had to give Script Editor the access in System Preferences > Security & Privacy > Privacy > Accessibility).
This is the code I am using to run the Apple Script from my app (via osascript):
let proc = Process()
proc.launchPath = "/usr/bin/env"
proc.arguments = ["/usr/bin/osascript","/Users/my_username/Desktop/test_apple_script.scpt"]
proc.launch()
This code works perfectly when I run a simple AppleScript that just displays a notification:
tell application "System Events"
display dialog "Test"
end tell
How can I make it so that my AppleScript runs from my macOS app just as it does when I run it in Script Editor?
Edit: For some more context, the command tab case here is just an example. I am trying to find a generalizable approach to simulating keyboard shortcuts by clicking buttons in my app (e.g., the user clicks on the "cut" button and the app acts as if the user has just pressed command + x). There may be a better approach to this than using an AppleScript, but I know that it is possible to do what I want using AppleScripts.
Edit: In case it wasn't clear from my original question, the simple AppleScript that displays a notification was already working from within my app. The problem arises when I try to run an AppleScript that simulates user input from the keyboard.
Edit: This code here worked for me temporarily, but it no longer works. I did not change the code at all. I simply ran my app again.
let PlayPauseScript = "tell application \"Spotify\"\n playpause\n end tell"\"test\"\n end tell"
if let scriptObject = NSAppleScript(source: PlayPauseScript) {
scriptObject.executeAndReturnError(nil)
}
Edit:
Here is where the problem currently stands. Based on suggestions, I added this to my info.plist, and now the Spotify AppleScript consistently works (when called via NSAppleScript):
<key>NSAppleEventsUsageDescription</key>
<string>...this is why I need permission...</string>
It seems my problem (now) is just with AppleScripts that use System Events. Despite making sure that both my app and osascript are allowed to control my computer (via security & privacy > privacy > accessibility), I now get the same error (1002) when using NEAppleScript and osascript. My app is also allowed to control System Events (security & privacy > privacy > automation). The osascript error prints to the console in Xcode, and I uncovered the error code for NEAppleScript by using this code (in the #IBAction of the button):
let script =
"""
try
tell application "System Events"
keystroke "c" using {command down}
end tell
on error errMsg number errorNumber
display dialog "An unknown error occurred: " & errorNumber as text
end try
"""
if let scriptObject = NSAppleScript(source: script) {
scriptObject.executeAndReturnError(nil)
}
The above code caused this to appear in the dialog box:
An unknown error occurred: 1002
When my app is run, it does prompt me to give it control over System Events, which I always do. My app is not sandboxed. I believe that both NSAppleScript and oascript did work for me once, but stopped working when I ran the app again (despite my not having made any changes to my code, I'm almost positive).
I don't know if this is informative, but the only other clue I have as to what might be going on is that I keep having to check and uncheck Script Editor's permission in security & privacy > privacy > accessibility to get it to run these AppleScripts because it tells me that Script Editor is not allowed to send keystrokes.
Security & Privacy -> Privacy tab -> Accessibility
Add /usr/bin/osascript
NOTE: You may also need to add your terminal (iTerm in my case)
To fix osascript 1002 permission issue.
Got to Security & Privacy -> Privacy tab -> Accessibility -> Add osascript (/usr/bin/osascript)
Or Use NSAppleScript
This is a couple of things for you to consider, more than a real answer to your question. If you'd like more details, I'll happily edit them in on request. (also, I'll put code examples in objective-C, because I'm still learning swift)
First, if you're going to be doing a lot of scripting in your app, it may be worth your while to switch over to Scripting Bridge. Scripting Bridge is a cocoa interface for applescripting other apps, so (to steal an example I found elsewhere) if you want to get the name of the current track in iTunes, you'd do something like:
iTuneApplication *iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
currentTrack = iTunes.currentTrack.name;
If you want specific code for Spotify I'll have to install that app; let me know.
Second, if you want to do keystrokes, you should look into Quartz Event Services. QES allows you to create, send, and capture input events. So if you want to send a ⌘C, you might do something like:
// '8' is the key code for 'c'
comCDownEvent = CGEventCreateKeyboardEvent(nil, 8, true);
CGEventSetFlags(comCDownEvent, kCGEventFlagMaskCommand);
comCUpEvent = CGEventCreateKeyboardEvent(nil, 8, false);
CGEventSetFlags(comCUpEvent, kCGEventFlagMaskCommand);
CGEventPostToPSN(targetAppProcessSerialNumber, comCDownEvent);
CGEventPostToPSN(targetAppProcessSerialNumber, comCUpEvent);
You can't run this code from sandboxed apps, but I think you'll have that problem any way you go.
The main advantage of these approaches is that they avoid switching contexts: it's all done from cocoa, so you don't need to invoke a shell or create an AppleScript environment, and avoid all the translation problems that involves.

Visual Foxpro exe file not working

I am beginner in using Visual FoxPro. I am trying to create a simple application which contains a form with a few textboxes. The problem is that the application window does not appear when I run the executable file which I build.
The code for the program is:
PROCEDURE main_app
_SCREEN.Activate
_SCREEN.WindowState = 2
_SCREEN.backcolor = RGB(128, 128, ;
128)
_SCREEN.Caption = ""
_SCREEN.refresh()
SET TALK OFF
SET DELETED ON
SET EXCLUSIVE OFF
CLEAR ALL
CLEAR
CLOSE ALL
DO FORM simpleform LINKED
READ EVENTS
RETURN
*MESSAGEBOX('Msg')
ENDPROC
In the form I have a few textboxes and a button. When the button is clicked then some code is executed. If I run the application from the IDE everything works fine but if I create the executable this does not display any window, still the process appears in the task manager. If I uncomment the MessageBox then this will appear even if I run the exe file. I don't know a way to trace the problem outside the IDE, maybe there is still a library needed that I am not aware.
I am using Visual FoxPro 9.0
Would someone be able to help me out?
If you can provide me with a link to a very simple application exe which uses just one form and finishes when the form is closed I would very much appreciate it.
Thanks in advance
I tried creating a brand new project, new code and new form just as you described. I copied your code and ran, but it never displayed the form.
If this is the main app in your project, you don't need the initial
PROCEDURE main_app
just remove this line as there is nothing CALLING the "Main_App" and thus is never getting called to ultimately call your form.
Just comment it out, save your project and run it... See what you get, it SHOULD work.
** PROCEDURE main_app
The problem was that I did not set the screen visibility to true.

How to handle bootstrap error in xcode 4.2,ios5.0

I did two sample applications in ios.Both applications i have done using storyboard.Then after i did i copied the two classes of one application to another.Then i saved and compiled,it worked>But second time when i build the application and when i run that application,am getting error like this:
Couldn't register BundleIdentifier.application name with the bootstrap server.Error:unknown
error code.
This generally means that another instance of this process was already running or is hung in the debugger.(gdb)
How to resolve this error?Please give me some suggestions in resolving this issue.
The reason for this error is that you are testing app in either simulator or device and suddenly change for another without stopping it.
For this you need to properly close app from navigator before moving for another ios selection.
if encounter this problem then restart xcode and simultor(also reset simulator).

Prevent closing the Application if file not saved

We have an application based on Eclipse-RCP. The problem I am trying to fix is as follows:
Say the user has an unsaved model file, and tries to close the application. The application rightly prompts the user for options to Save the file, Ignore it, or Cancel closing the application. Ignore and cancel are no-brainers. If the user decides to save the file, there is another dialog box that gives the user an option to either save the file or cancel the save (its a custom editor, similar to a Save-As dialog).
Now, the question:
How do I prevent killing the application when the user selects "Yes" in the first prompt, but cancels the save in the second? I thought of looking up the base class of the application, but couldn't find it. Or should I be looking at the custom-editor for the model file?
Thanks in advance, any help is appreciated...
If you are in RCP you have the luxury of returning false from WorkbenchAdvisor.preShutdown()
Let a workbench part holding the model implement org.eclipse.ui.ISaveablePart2 interface meant for cases like yours.