Copy link to clipboard
Copied
Hello,
I wrote a script that builds collections from seach results - all working as expected.
The part that I didn't figure out was how to select and display the collection after it has been created.
I couldn't find a document method for it, .active or .focus don't seem to work.
Thanks
Antoine
#target bridge
app.bringToFront();
var userColls = app.getCollections();
var coll = getCollection( userColls, 'Testing' );
app.document.thumbnail = coll;
//
function getCollection( collsArr, nameStr ) {
for ( var i = 0; i < collsArr.length; i++ ) {
if ( collsArr.name == nameStr ) { return collsArr; };
};
return null;
};
The App has a getCollections method that returns an Array of thumbnails, just loop that with an if statement. Set the current document/window thumbnail… done
...Copy link to clipboard
Copied
#target bridge
app.bringToFront();
var userColls = app.getCollections();
var coll = getCollection( userColls, 'Testing' );
app.document.thumbnail = coll;
//
function getCollection( collsArr, nameStr ) {
for ( var i = 0; i < collsArr.length; i++ ) {
if ( collsArr.name == nameStr ) { return collsArr; };
};
return null;
};
The App has a getCollections method that returns an Array of thumbnails, just loop that with an if statement. Set the current document/window thumbnail… done..?
Copy link to clipboard
Copied
Thanks Mark, It works wonders!
Copy link to clipboard
Copied
I'm posting here to keep the board clean.
I have a follow up question, regarding the same script but another difficulty.
It's a question regarding event handlers and their scope.
I'm trying to navigate to a folder and select a number of files in there.
It seems there is no way around using an event handler, as the selection has to happen after the document load.
However i don't want it as a repeating behavior (not after every document load).
I was able to write a script that works, although using a logic that I haven't seen in other scripts or templates:
function listJpeg(){
// Array to hold Jpeg list
var JPGs = new Array();
// for selected items, guess jpeg location and push into JPGs array
if (app.document.selectionLength > 0){
var thumbs = app.document.getSelection("psd,jpg,png,swf,fla,ai");
for (var i = 0; i < thumbs.length; i++){
var JPG = findJpeg(thumbs); // guesses the location of jpeg file and returns a thumbnail on success
if (JPG != null) { JPGs.push( JPG ) };
}
}
// navigate to the JPEG folder and select the files after the document has loaded
if (JPGs.length > 0) {
function onDocLoaded( event ){
if ( event.object instanceof Document && event.type == 'loaded' ) {
for (var i = 0; i < JPGs.length; i++){ app.document.select (JPGs) };
app.eventHandlers.pop();
}
return { handled: false };
}
app.eventHandlers.push( { handler: onDocLoaded } );
app.document.thumbnail = JPGs[0].parent;
}
}
listJpeg();
Is this a safe and sound solution to my problem?
I'm not an experienced developper and I can't tell whether this is bad practice.
It seems that declaring the event handler inside the listJpeg function allows it to remember it's scope?
I'm also deleting the event handler from within it, to force it to run only once...
I also can't figure out what the "return {handled:false};" line does?
best
antoine
Copy link to clipboard
Copied
Hey start a new topic… The forum could use the traffic with your posts that makes about 5 of us here…!!
Explain further what you are trying to do here…
You can script *.jpeg filtering by script text or regexp methods…
Copy link to clipboard
Copied
Hey Mark, thanks for the answer.
I'll post the next question as a new topic.
The script looks for proxy files (JPEG) that are stored in a different folder than the working files (PSD).
The logic should be simple: get selection -> find matching JPEG for each selected file -> go to JPEG folder and select corresponding files.
The part I am unsure about is how to navigate to the JPEG folder then select the corresponding files.
My first issue is how to create an event handler that has access to the scope of my function, it needs to know the array of files to select.
I solved that by declaring the handler inside my function, which seems to work. Is that a good solution?
The second issue is how to let the handler know when we need to select the files. Obviously I don't want this to happen every time I open a folder.
I solved that by un-registering the handler at the end of it's function, that makes it a single use handler? This seems dodgy as the pop method doesn't take an argument and I can't specify which handler to pop.
I also don't know whether it's good to register a new handler every time I run the script? Is it slow? will they add up in the memory?
Lastly, I'm unsure of what the line "return { handler:false }" does, it's found at the end of every event handler.
I've edited the code to help:
function select_proxys(){
// get selection
if (app.document.selectionLength > 0){
var thumb_array = app.document.getSelection("psd,jpg,png,swf,fla,ai");
}
// find corresponding proxys
var proxy_array = find_proxys( thumb_array );
if (proxy_array > 0)
{
// define event handler function
function onDocLoaded( event ){
if ( event.object instanceof Document && event.type == 'loaded' )
{
// select the proxy thumbnails
for (var i = 0; i < proxy_array.length; i++){
app.document.select (proxy_array)
};
// un-register the event handler
app.eventHandlers.pop();
}
return { handled: false };
};
// register the event handler
app.eventHandlers.push( { handler: onDocLoaded } );
// navigate to the proxy parent folder, triggering the event handler.
app.document.thumbnail = proxy_array[0].parent;
};
};
select_proxys();
Copy link to clipboard
Copied
If I was wanting to do this then I would add either a menu item or contextual menu…?
Event handlers are going to kick off all the time and turning them on/off will be APITA…?
Are the file paths between the *.psd files and *.jpg files always relative…?
Copy link to clipboard
Copied
This script is already being launched from a contextual menu, and the paths are always relative.
I'm using an event handler because that's what recommended in the documentation in relation to the "document.select()" method.
The select method only works reliably after the document has finished loaded, so I'm listening for that event.
not sure what APITA means, apologies.
Copy link to clipboard
Copied
A pain in the ar$e…
Copy link to clipboard
Copied
Well you're not wrong about that.