OrderedBytes
OrderedBytes User Forum
FAQ FAQ     Search Search     Memberlist Memberlist     Usergroups Usergroups     Register Register
Profile Profile     Log in to check your private messages Log in to check your private messages     Log in Log in
Sole CM helper won't run certain Applescript blocks
Display posts from previous:         View previous topic :: View next topic
Post new topic Reply to topic Subscribe to this topic    The OrderedBytes Forum -> Support
Sole CM helper won't run certain Applescript blocks Mon Jan 08, 2018 5:13 am  •   #11860
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

Applescripts containing dialogs only run if ControllerMate editing software is also running, rendering the whole thing pointless.
With the ControllerMate helper applet running only, Applescripts with dialogs will not run, but other Applescripts will.
I have checked “allow the user to interact with the script” from the Applescript block’s properties. I have also gone to System Preferences > Security and Privacy, and re-enabled UI scripting for ControllerMate and the helper. Neither attempt fixed the problem.

Is this a bug or am I missing something?
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5


Last edited by R7 on Fri Jan 12, 2018 3:22 am; edited 1 time in total
View user's profile Send private message Reply with quote
Mon Jan 08, 2018 2:49 pm  •   #11862
Ken
Developer


Joined: 27 Mar 2006
Posts: 4016

Can you post the script that isn't being performed by the helper?
_________________
Ken
www.orderedbytes.com - www.controllermate.com
ControllerMate -- Programming controllers for Mac OS X since 2005.
View user's profile Send private message Visit poster's website Reply with quote
Tue Jan 09, 2018 6:30 am  •   #11864
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

Actually I just got some more information on this case:

I noticed that even non-dialog Applescripts weren't running, so I found from Activity Monitor that ControllerMateHelper wasn't even running, it had crashed at some point.
However, relaunching the helper, I still have a problem with the Applescripts that contain dialogs. Non-dialog scripts run fine, and so do scripts that contain "display notification" instead of "display dialog". The new found extra symptoms are as follows:

I double click to launch ControllerMate Helper and see it appear to Activity Monitor.
Then I press the button that will launch the Applescript that contains dialog. Nothing appears on the display, the dialog isn’t behind or in front of any window, and I don’t have extra desktop spaces.
But now if I try to double click ControllerMateHelper.app in Finder, I get the error sound. It suggests that the Applescript dialog “is” somewhere and it’s expecting to be clicked but I can’t see it.
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5


Last edited by R7 on Fri Jan 12, 2018 3:22 am; edited 1 time in total
View user's profile Send private message Reply with quote
Fri Jan 12, 2018 3:18 am  •   #11872
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

Ken wrote:
Can you post the script that isn't being performed by the helper?


Please see my previous post for more info but, sure. It happens with any and every Applescript that contains display dialog.

For example, I can replicate the problem with this script:

Code:
display dialog "What will you choose?" buttons {"A", "B", "C"} giving up after 60
set the button_pressed1 to the button returned of the result

if (the button_pressed1 is "A") then
   display notification "" with title "You chose A."
   say "You chose, A"
else if (the button_pressed1 is "B") then
   display notification "" with title "You chose B."
   say "You chose, B"
else if (the button_pressed1 is "C") then
   display notification "" with title "You chose C."
   say "You chose, C"
end if


And the block looks like this:



It's very simple: This script runs if ControllerMate is running, but nothing shows up if I quit the program is ControllerMateHelper is the only one running.
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5
View user's profile Send private message Reply with quote
Mon Jan 15, 2018 6:04 am  •   #11877
dabo



Joined: 19 Feb 2014
Posts: 51
Location: Los Angeles

I’m wondering if the script is failing because it’s confused about the target application or application process it should be addressing.

Wrap the entire script inside a System Events tell block and see if it works.

tell application “System Events”

— your script here

end tell
View user's profile Send private message Reply with quote
Solved! Mon Jan 15, 2018 11:42 am  •   #11879
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

dabo wrote:

Wrap the entire script inside a System Events tell block and see if it works.

tell application “System Events”

— your script here

end tell


Thank you so much! This works! As a temporary workaround I had created an Applescript block to which I simply wrote a shell script command to run the desired Applescript app file in a certain path. While it worked, it seemed a bit clumsy and it would cause the Script Editor app to make an appearance in the dock. Your suggestion fixes the script and they now run neatly in the background as they should.

I was increasingly concerned about this because for many of the Applescripts that contain dialog, I have planned to use custom dialog interaction switches that I'll be building; For that it's crucial that the dialog not only happens, but it comes visible and active so I can send a key vombination to it that activates the capability to use the keyboard to answer to the dialog. Here's the final test script, see: When the script comes up, you should be able to use the tab key to cycle through the options and then hit space to activate it. For my physical A-B-C switches, I could attach building blocks that emulate space, tab + space, and tab + tab + space.

Code:
tell application "System Events"
   tell application "System Events" to key code {63, 98} using (control down)
   delay 0.3
   display dialog "What will you choose?" buttons {"A", "B", "C"} giving up after 60
   set the button_pressed1 to the button returned of the result
   tell me to activate
   if (the button_pressed1 is "A") then
      tell application "System Events" to key code {63, 98} using (control down)
      display notification "" with title "You chose A."
      say "You chose, A"
   else if (the button_pressed1 is "B") then
      tell application "System Events" to key code {63, 98} using (control down)
      display notification "" with title "You chose B."
      say "You chose, B"
   else if (the button_pressed1 is "C") then
      tell application "System Events" to key code {63, 98} using (control down)
      display notification "" with title "You chose C."
      say "You chose, C"
   end if
end tell



Ken, if this hasn't been mentioned in the documentation yet, dabo's tip is precious info for Applescript block users. Cool
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5
View user's profile Send private message Reply with quote
Tue Jan 16, 2018 5:11 am  •   #11880
dabo



Joined: 19 Feb 2014
Posts: 51
Location: Los Angeles

What application are you controlling with these scripts?
Your script targeting needs to be cleaned up for the notification and dialog windows appear properly,

For example, if this.were to be used to control an application named “MyApp” it would look like this:


tell application "System Events"
tell process “MyApp”
key code {63, 98} using (control down)
delay 0.3
display dialog "What will you choose?" buttons {"A", "B", "C"} giving up after 60
set the button_pressed1 to the button returned of the result
set.frontmost to 1
if (the button_pressed1 is "A") then
key code {63, 98} using (control down)
display notification "" with title "You chose A."
say "You chose, A"
else if (the button_pressed1 is "B") then
key code {63, 98} using (control down)
display notification "" with title "You chose B."
say "You chose, B"
else if (the button_pressed1 is "C") then
key code {63, 98} using (control down)
display notification "" with title "You chose C."
say "You chose, C"
end if
end tell
end tell
View user's profile Send private message Reply with quote
Tue Jan 16, 2018 10:28 am  •   #11883
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

dabo wrote:
What application are you controlling with these scripts?
Your script targeting needs to be cleaned up for the notification and dialog windows appear properly,
end tell


With these, none in particular, actually; The dialogs are created by OS X itself. Many of my scripts are quite multi-faceted, carrying out an array of tasks that involve logical deduction or sometimes even going through answering to multiple dialog questions until it really decides what to do. There might be maths, time and date related stuff, parsing variables from text files or .plist files or JSON, dropping some shell script to send out JSON and so on. With my example above, I already got the script to work as I intended and I have never needed to add "tell process “MyApp”" before...

On random circumstances however, I might've launched one of those special and complex scripts that also contain dialog, and occasionally the dialog would get buried behind other application windows. When they were merely launched as Applescript apps, the simple way to bring the dialog foremost was just to click the app's icon in dock. But when ControllerMateHelper runs scripts like this, they don't appear in the dock, so the script itself must contain the extra line(s) that force the dialog to become visible for real. "Tell application "System Events"" was the key there.

I also have much simpler scripts and hot keys (up until now programmed and launched with BetterTouchTool) that are only available when certain applications are foremost, and they trigger processes within that application only. There of course the scripts must be addressed to that specific application in order to work.
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5
View user's profile Send private message Reply with quote
Re: Solved! Wed Jan 17, 2018 6:50 am  •   #11890
Ken
Developer


Joined: 27 Mar 2006
Posts: 4016

R7 wrote:
Ken, if this hasn't been mentioned in the documentation yet, dabo's tip is precious info for Applescript block users. Cool

Yeah, that is a good tip. I'll add that to the documentation.
_________________
Ken
www.orderedbytes.com - www.controllermate.com
ControllerMate -- Programming controllers for Mac OS X since 2005.
View user's profile Send private message Visit poster's website Reply with quote
Re: Solved! Wed Jan 17, 2018 12:05 pm  •   #11895
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

And by the way, I noticed that the line "tell application "system events"" for dialogs also solves the problem that if I used a key to launch such Applescript while I had something in fullscreen, I'd have to manually return from fullscreen mode and reactivate it after it again. I can't describe how much I hate that OSX feature that fullscreen is now a space that makes that nauseating and awkwardly clumsy slide animation!
So now, no need to exit fullscreen just to handle some dialog on the go! Cool


Ken wrote:
R7 wrote:
Ken, if this hasn't been mentioned in the documentation yet, dabo's tip is precious info for Applescript block users. Cool

Yeah, that is a good tip. I'll add that to the documentation.


Thanks!
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5
View user's profile Send private message Reply with quote
Wed Jan 17, 2018 6:38 pm  •   #11896
dabo



Joined: 19 Feb 2014
Posts: 51
Location: Los Angeles

R7,

The System Events tell block wrapper was a fix for your example script and should not be a global suggestion in the documentation.
It was merely a diagnostic in troubleshooting your specific problem.

The reason the scripts worked when the ControllerMate programming window was open, or when the AppleScript editor was open is that in those contexts, there is an assumption that a line of scripting without a target defined by a tell block header is addressed to the script editor application process. When an editor was not open, your script had no defined target. This will lead to dialog windows opening behind the frontmost process.

If your scripts are properly written and targeting the right thing at the right time they will run with dialogs appearing as intended.
Upon dimissing the dialog, control will return to the intended target application.

I asked what application you were addressing with your scripting because there are times when you need to target System Events and times when you need to target the application process you are controlling or even a specific window in an application process for dialogs to be frontmost and visible.

Other notes:
Your revised version of the script does not need the added ‘tell application “System Events”’ lines in the body of the script because the entire script targets System Events.

Consider using the “choose from a list” AppleScript dialog rather than three buttons.
View user's profile Send private message Reply with quote
Thu Jan 18, 2018 8:37 am  •   #11899
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

dabo wrote:
The System Events tell block wrapper was a fix for your example script and should not be a global suggestion in the documentation.
It was merely a diagnostic in troubleshooting your specific problem.

The reason the scripts worked when the ControllerMate programming window was open, or when the AppleScript editor was open is that in those contexts, there is an assumption that a line of scripting without a target defined by a tell block header is addressed to the script editor application process. When an editor was not open, your script had no defined target. This will lead to dialog windows opening behind the frontmost process.

If your scripts are properly written and targeting the right thing at the right time they will run with dialogs appearing as intended.
Upon dimissing the dialog, control will return to the intended target application.

I asked what application you were addressing with your scripting because there are times when you need to target System Events and times when you need to target the application process you are controlling or even a specific window in an application process for dialogs to be frontmost and visible.

I mean that the System Events tell block wrapper does fix the problem of display dialog not having become visible. What’s wrong with that? Display dialog is created by OS X, not the kind of application that you can find from the Applications folder for example. As a context-unaware dialog, I think the tip is quite valid to be included in the documentation, because based on the current documentation only, the scripts that had previously worked perfectly, now didn't work in ControllerMateHelper because this piece of information was crucial to know.

If I map an Applescript with nothing but a dialog into a hotkey (and now, wrap the dialog into the System Events tell block), then the dialog will appear on top of other windows and completely unaware of whatever application I previously had in front of me. Those are the very circumstances under which I mostly will be triggering the keys that launch dialogs. Any time, any place.

But you’re right about the difference of a script working when ControllerMate programming window or Script Editor was open, versus running an AppleScript application as a standalone; I’m quite familiar with the phenomenon of how some script applications only run in Script Editor, but won’t as applications, and then they require some alterations. It comes up sometimes, calling for some online research.

Naturally, if I write an AppleScript that’s meant to, say, control Safari or iTunes or whatever, within the rules that the scripting dictionaries of the applications have, then of course I’d have to use “tell application “Safari”” instead. But now that the point is dialogs, that isn’t helpful unless that dialog launcher hotkey is one that is specifically available only when Safari is open, and that dialog is about some Safari related task and nothing else.
For iTunes, I often use a bunch of scripts by Doug, and since they’re only in iTunes, even though I can’t get to see what he’s written in the scripts, it’s possible that he tells iTunes to display a dialog and not just in general.
But what dialog containing scripts I have made, they’re always general and unrelated to applications; They’re about setting up timers and other transparent background stuff.

dabo wrote:
Other notes:
Your revised version of the script does not need the added ‘tell application “System Events”’ lines in the body of the script because the entire script targets System Events.

Are you referring to the key code lines?

dabo wrote:
Consider using the “choose from a list” AppleScript dialog rather than three buttons.

Thanks, I use the list when there are either more than 3 options to choose from, or the options consist of 3 or more words each, stretching the buttoned window out of proportions. Most of the time this is not necessary, and the usual three button layout allows me to use a custom icon for the dialogs, which is a big help in intuitive comprehension of what the question is about.
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5
View user's profile Send private message Reply with quote
Sat Jan 27, 2018 11:11 am  •   #11945
viscount



Joined: 19 Jan 2016
Posts: 7

Using "System Events" didn't work for me; however running the script from an app made with automator did the trick (I called the app using the Finder block in CM8).
View user's profile Send private message Reply with quote
Sat Jan 27, 2018 12:53 pm  •   #11947
R7



Joined: 07 Jan 2018
Posts: 31
Location: Finland

I've learned to write very efficient and tedious Applescript apps by now but to this day I can't use Automator at all! Razz The various occasional attempts that I had it with, somehow the applets just wouldn't run correctly or Automator was too unintuitive to use, so it was better to learn Applescript instead. I had no previous scripting language knowledge so... I guess Automator came off as a particularly unfriendly piece of software, now that you think of it. Very Happy

/offtopic
_________________
ControllerMate for X-Keys, P.I Engineering Matrix Controller Board, Mac Pro 5.1 running Mac OS X 10.10.5
View user's profile Send private message Reply with quote
Page 1 of 1 All times are GMT - 6 Hours
Post new topic Reply to topic Subscribe to this topic    The OrderedBytes Forum -> Support
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Copyright © 2005 — 2012 OrderedBytes
All rights reserved.