Copy link to clipboard
Copied
Let's say Photoshop has 10 opened documents and a one-line custom script with the command app.activeDocument.close()
When the script is triggered using a keyboard shortcut the active document is closed.
When the keyboard shortcut is pressed down continuously all 10 documents will close in a row.
What needs to take place so that only one document is closed at a time when the script runs?
Independently of the keyboard shortcut key down duration?
PS2022
OSX Catalina
Copy link to clipboard
Copied
Haven't tested it, but check it out.
app.activeDocument.close()
while (ScriptUI.environment.keyboardState.keyName) { app.refresh(); }
Copy link to clipboard
Copied
Thanks r-bin. I try the snipet with the F2 key and still all open docs close when the F2 key is pressed for 1or 2 seconds.
app.activeDocument.close()
while (ScriptUI.environment.keyboardState.F2) { app.refresh(); }
Is ther a differnt way to identify the key name? and would you handle a keybord short cut combination like option+command+F2?
Copy link to clipboard
Copied
Why did you replace keyName with F2?
keyName is a property of the object (variable).
The code is ready, nothing needs to be changed. Suitable for any key.
P.S. ~(_8^(|)
Copy link to clipboard
Copied
The code still closes all the open docuemnts when the keyboard shortcut is presssed for more thant a fraction of a second. I thougthat by changing the keyName I would fix that but it did not.
On a mac system with osx Catalina, the code closes all the open documents when the key ir presses down fro 1 sec.
Copy link to clipboard
Copied
The code still closes all the open docuemnts when the keyboard shortcut is presssed for more thant a fraction of a second. I thougthat by changing the keyName I would fix that but it did not.
On a mac system with osx Catalina, the code closes all the open documents when the key ir presses down fro 1 sec.
By @Polycontrast
Copy link to clipboard
Copied
Yesterday when I saw the topic, I wrote the following code. But your solution with reading the key codes seemed to me more suitable
if (!$.getenv('timer') || ((new Date()).getTime()-$.getenv('timer')) >5000) {app.activeDocument.close(); $.setenv('timer', (new Date()).getTime())}
Copy link to clipboard
Copied
Thank you Jazzy and r-bin for your help wit solving this keyboard shortcut issue.
The timer idea works well in a mac system. In the OS Catalina the script UI environemtns didn't work as expected but I get the impression that it works in the windows environment.
Copy link to clipboard
Copied
The code still closes all the open docuemnts when the keyboard shortcut is presssed for more thant a fraction of a second. I thougthat by changing the keyName I would fix that but it did not.
On a mac system with osx Catalina, the code closes all the open documents when the key ir presses down fro 1 sec.
By @Polycontrast
Also try this option. I wonder how it works for you.
if (ScriptUI.environment.keyboardState.keyName) app.activeDocument.close();
while (ScriptUI.environment.keyboardState.keyName) app.refresh();
Copy link to clipboard
Copied
I am thinking a for loop can allow to exit the script after the active docuemnt is closed.
How can I modify this loop so that it loops in reverse from the total numeber of open docs to 0?
For example if 3 docs are open
The loop returns
2
1
0
for(var i=0; i < app.documents.length; i++) {
alert(i)
}
Copy link to clipboard
Copied
луп не поможет.
скрипт закроет все документы если его вызовут 100 раз из-за того, что клавиша запала.
P.S. нажимайте ctrl+w (или задайте свою комбинацию).
Find more inspiration, events, and resources on the new Adobe Community
Explore Now