Copy link to clipboard
Copied
Hello,
My question is, how can I find out with AppleScript whether I have a selection active or not?
I'll give a quick overview of my use case, so you know what I need it for:
I'm digitally painting with a Wacom tablet with four buttons and a modifier on the pen. Given that four buttons aren't enough, I'm using KeyboardMaestro to double-map them. I've mapped them to F1-F4, and they are doing different things whether I press the button on the pen (which I have mapped to Option) or not.
I haven't found a way to send a key command ignoring a pressed modifier, so I have to queue up actions and then fire them all at once when the modifier is released. Since I'm basically doing while (modifierDown) sleep 250 in the script, the queue isn't exactly sophisticated, so that occasionally leads to several filters and a deselect being in there and stuff being mixed up. While the solution isn't optimal, it's the best I've come up with.
My idea would be to not make the filter command trigger via keyboard maestro if there's no selection active in photoshop... or maybe some of you has an idea how to fix the other thing?
Awesome, so my solution is this:
tell application "Adobe Photoshop CC 2015" to set result to do javascript "function isSelectActive(){ var res = 0; var activeLayer = activeDocument.activeLayer; var as = activeDocument.activeHistoryState; activeDocument.selection.deselect(); if (as != activeDocument.activeHistoryState) { res = 1; activeDocument.activeHistoryState = as; } activeDocument.activeLayer = activeLayer; return res; } isSelectActive()"
tell application "Ad
...Copy link to clipboard
Copied
I have seen many javascripts here for has selection perhaps there are some for applescript
https://forums.adobe.com/search.jspa?sort=updatedDesc&place=%2Fplaces%2F1383833&q=hasselection
Copy link to clipboard
Copied
So there's no way to do it with AppleScript, I have to use JavaScript?
Copy link to clipboard
Copied
Did you look at the Search I posted? Applescript I believe can also use some javascripting.
Copy link to clipboard
Copied
I did. Tried to throw some of these into the AppleScript editor, and it gave me syntax errors, so I figured it was JavaScript. For example, it wouldn't let me define functions.
Edit: Oh, I just saw that you edited your answer, it's clearer now. Before it contained just the link, so I figured that you considered that exhaustive and wondered what it was that I wasn't seeing.
Copy link to clipboard
Copied
A function someone wrote it should be in the script code that your looking at
function hasSelection(doc) {
if (doc == undefined) doc = activeDocument;
var res = false;
var activeLayer = activeDocument.activeLayer; // remember active layer
var as = doc.activeHistoryState; // get current history state #
doc.selection.deselect();
if (as != doc.activeHistoryState) { // did the deselect get recorded
res = true;
doc.activeHistoryState = as; // backup and reselect
}
activeDocument.activeLayer = activeLayer; // insure active layer didn't change
return res; // return true or false
}
Copy link to clipboard
Copied
I tried exactly that function. Until a minute ago, I didn't know that do javascript "theJavaScript" was a thing. Thank you!
Copy link to clipboard
Copied
Awesome, so my solution is this:
tell application "Adobe Photoshop CC 2015" to set result to do javascript "function isSelectActive(){ var res = 0; var activeLayer = activeDocument.activeLayer; var as = activeDocument.activeHistoryState; activeDocument.selection.deselect(); if (as != activeDocument.activeHistoryState) { res = 1; activeDocument.activeHistoryState = as; } activeDocument.activeLayer = activeLayer; return res; } isSelectActive()"
tell application "Adobe Photoshop CC 2015"
set result to do javascript (file "/Users/me/hasSelect.jsx")
if result contains "true" then
set r1 to 1
else
set r1 to 0
end if
end tell
(I'm returning 0 or 1 because it's easier to see in KeyboardMaestro.)
function hasSelection(doc) {
if (doc == undefined) doc = activeDocument;
var res = false;
var activeLayer = activeDocument.activeLayer; // remember active layer
var as = doc.activeHistoryState; // get current history state #
doc.selection.deselect();
if (as != doc.activeHistoryState) { // did the deselect get recorded
res = true;
doc.activeHistoryState = as; // backup and reselect
}
activeDocument.activeLayer = activeLayer; // insure active layer didn't change
return res; // return true or false
}
hasSelection();
Couldn't have done with without @JJMack!