• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Keyboard shortcut to execute specific function from script

Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

I work with custom Photoshop scripts and keyboard shortcuts.

Is it possible to have a keyboard shortcut execute a specific function within a script?

and, another keyboard shortcut executes another function in the same script?

 

For example?

Citrus.jsx

  • function pomelos(){}
  • function lemons(){}
  • function oranges(){}

 

Keyboard F1 executes the function Pomelos

Keyboard F2 executes the function Lemons

Keyboard F3 executes the function Oranges

 

 

 

TOPICS
Actions and scripting

Views

7.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

LEGEND , May 30, 2021 May 30, 2021

Assiang F1 key to the script, basicly for pomelos function. When you want to trigger lemons then during refreshment change your finger from F1 key to F2. Similary if you want to initiate oranges, switch to F3.

 

refresh()

function pomelos() {alert(callee.name)}
function lemons() {alert(callee.name)}
function oranges() {alert(callee.name)}

({1: pomelos, 2: lemons, 3: oranges})[ScriptUI.environment.keyboardState.keyName[1]]()

 

Votes

Translate

Translate
Valorous Hero , May 31, 2021 May 31, 2021

[Profanity removed]

Remove the first test alert() and refresh() from the script.

Votes

Translate

Translate
Adobe
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Assiang F1 key to the script, basicly for pomelos function. When you want to trigger lemons then during refreshment change your finger from F1 key to F2. Similary if you want to initiate oranges, switch to F3.

 

refresh()

function pomelos() {alert(callee.name)}
function lemons() {alert(callee.name)}
function oranges() {alert(callee.name)}

({1: pomelos, 2: lemons, 3: oranges})[ScriptUI.environment.keyboardState.keyName[1]]()

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

It seems to me that there should be an endless loop. Not?

 

Also swears at the line:

({1: pomelos, 2: lemons, 3: oranges})[ScriptUI.environment.keyboardState.keyName[1]]()

undefined is not an object

upd.

keyName == undefined

 

upd2.

It seems I misunderstood what the script does. Not a reliable method. And, if you remember, you can assign any number of hotkeys to the script, then everything is easier, but there can also be problems.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Basicly save script to Presets / Scripts, relaunch Photoshop and assign F1 to appropriate item from File / Scripts. Then press F1 (for one second, that's the time of refresh()) so the script is going to call pomelos function. If you press F1 and then switch your finger to F2 or F3 for the time of lasting second it is going to call lemons or oranges.

 

I used it for some project and never had endless loop, and can't say why it could be unreliable if it always worked for me. To avoid one second of waiting after pressing the function key, you can combine it with BridgeTalk.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Yes I understand. It is not comfortable. I can not. And it must be remembered that you need to keep the key (even F1) pressed for a while.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Implementing BridgeTalk shortens time of awaiting for the result to milliseconds.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Don't you consider the option with several hotkeys?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Personally I used it fundamentally as F1 (for main part of script), also together with Shift, Alt or Ctrl for special options, or else combinations of them, when holding more keys beside F1.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Thank you! I will investigate your suggestion and let you know how it goes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

I use 5 separate script files that apply the same function to 5 separate Photoshop layers. 

Each script has its own keyboard combination key.

If the layer name is x then do function x.

 

I am trying to understand if it is possible to combine all 5 scripts into one main script and use separate keyboard shortcuts to execute a specific section of the main script. 

 

The Problem

Multiple script files

 

The solution 1

One script that responds to 5 separate  keyboard shortcuts

The solution 2

A loop that can detect the keyboard shortcut input and executes a function for the keyboard shortcut 

 

The benefit

One script file instead of 5 scripts 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Have you considered this option?

000.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Does it require building a UXP Photoshop panel?

And how would each button link to a specific function within the script?

Similar to assigning a unique ID to the button and the function to trigger the function? 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

quote

Does it require building a UXP Photoshop panel?

And how would each button link to a specific function within the script?

Similar to assigning a unique ID to the button and the function to trigger the function? 

 

 


By @Polycontrast

 

 

var f = null;
var d = new Window("dialog")

d.orientation = "row";

d.b1 = d.add("button", undefined, "Func1");
d.b2 = d.add("button", undefined, "Func2");
d.b3 = d.add("button", undefined, "Func3");
d.b4 = d.add("button", undefined, "Func4");
d.b5 = d.add("button", undefined, "Func5");

d.b1.onClick = function() { f = func1; d.close(); }
d.b2.onClick = function() { f = func2; d.close(); }
d.b3.onClick = function() { f = func3; d.close(); }
d.b4.onClick = function() { f = func4; d.close(); }
d.b5.onClick = function() { f = func5; d.close(); }

d.addEventListener("keydown", key_handle, false);   

d.b1.active = true; // otherwise a glitch 

d.show()

if (f) f();

function key_handle(e)
    {
    switch (e.keyName)
        {
        case "F1": d.b1.notify(); break;
        case "F2": d.b2.notify(); break;
        case "F3": d.b3.notify(); break;
        case "F4": d.b4.notify(); break;
        case "F5": d.b5.notify(); break;
        }
    }

function func1() { alert(1); }
function func2() { alert(2); }
function func3() { alert(3); }
function func4() { alert(4); }
function func5() { alert(5); }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

He asked for shortcuts, not buttons 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Thank you for the suggestion. The button panel would work best if the panel is a floating or dockable panel. I believe this can be done with a UXP panel.  Is possible to assign a keyboard shortcut to a UXPl panel button? 

This way the function can be triggered through the UI button or keyboard shortcut. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

To Polycontrast

You know more than me. Why don't you try it yourself? What is "UXP panel"?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

UXP is something more experienced than that I think you didn't like, so CEP.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

You already explained in first post what you wish, and you got appropriate script. It does that you want, so what is problem? Did you even try it? That does exactly you are describing now.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

I have not to try yet, first, I must interpret it to figure out of to apply your suggestion.  It is not a simple process for me. Once I try it I can share my findings.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Hmm, the script is finished, just replace content of functions for yours.

But before you replace anything inside, just run it to see how it works...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

I try the citrus.jsx script in Photoshop CC 2021 and get error 21 when I press the F1 key in a mac.

Then I get several erro 24 dialogs when I press F1 ket for one sec and then switch to F2 key.

What do these errors mean?

 

Screen Shot 2021-05-30 at 10.38.28 PM.pngScreen Shot 2021-05-30 at 10.39.07 PM.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Insert into your script in the first line

alert (ScriptUI.environment.keyboardState.keyName)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

This is an interesting idea which I am not sure how it works.
The first F1 press produces the F1 alert followed by the Error 21 alert.
I was able to get the Lemons alert only once.
 
Screen Shot 2021-05-31 at 11.36.04 AM.pngScreen Shot 2021-05-31 at 11.35.17 AM.pngScreen Shot 2021-05-31 at 11.35.24 AM.pngScreen Shot 2021-05-31 at 11.30.54 AM.png
 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Why don't you like my script? The keys f1-f5 also work there.

You can also assign the same f1 key to the script call.

There is no need to do any special delays or tricks with your fingers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines