Look for specific document tab name and switch to it, if it doesn't exist, open a doc at custom filepath

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

asdfg9
Posts: 1
Joined: Thu Aug 05, 2021 4:51 pm

Look for specific document tab name and switch to it, if it doesn't exist, open a doc at custom filepath

Post by asdfg9 »

Hello, would anyone be so kind and wrote me a very simple script that:
Goes through all opened document tab names (including floated document windows (which can potentially have multiple tabs too)) looking for a tab with a name that contains the string from my desired_tab_name variable. If it exists, switch to it, if it doesnt exist, open a file on disk at a filepath from my desired_doc_filepath.

Pseudo code:

Code: Select all

desired_tab_name = "tabname"		// look for this in opened tabs and floated document window names
desired_doc_filepath = "filepath"	// this filepath gets opened if the desired tabname wasnt found

found_tab_id = -1
loop ( all opened document tab names (including floated document windows) )
(
	if (looped tab name contains $desired_tab_name)
	(
		found_tab_id = ?api?	// get the tab's index or whatever ps uses to then switch to the tab
		end loop
	)    
)

if ( found_tab_id != -1 )		// something was found and overwrote the default value
	( switch to the tab's id )
else
	( open file at $desired_doc_filepath )
and yes i want to keep those two variables separate and unrelated. dont want to derive the tab name off the filepath or anything, as i will do that externally with autohotkey to automatically modify the two variables and launch the script for like 300docs i need to instantly access with hotkeys for each, usualy 50 open at same time. in pre-2021 photoshop i could just run the file on disk and photoshop would switch to the tab if its already open, but in 2021 theres problems when doing this with massive files. and so new version means new workaround.

big thanks if anyone helps, i cannot possibly imagine messing with yet another language and api manual right now
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Look for specific document tab name and switch to it, if it doesn't exist, open a doc at custom filepath

Post by Kukurykus »

Code: Select all

nme = 'someDocument.jpg'; if (lngth = (dcmnts = documents).length) {
	for(i = 0; i < lngth; i++) {if ((dcmnt = dcmnts[i]).name == nme) {activeDocument = dcmnt; break}}
	if (i == lngth) (fle = File('~/desktop/customFileToOpenFromTheDesktop.jpg')).exists && open(fle)
}