But it cannot reveal and select multiple files in Finder.
I have managed to make a Keyboard Maestro Macro using some AppleScript to reveal and highlight say, three files in Finder if I pass their paths to the AppleScript. But I have not been able to make this work with passing any number of files to the AppleScript. For example, if I have a Keyboard Maestro Macro that matches a few files in a folder I can get Keyboard Maestro to generate a list of these files and their paths but I cannot work out a way to pass this list to AppleScript...
In other words, I want to be able to generate a list of files (they are all in the same folder) in Keyboard Maestro and pass this list as a single Variable to AppleScript so I can reveal and highlight them all in the Finder. Any help appreciated!
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set Path1 to getvariable "LOCAL__Path1" instance kmInst
set Path2 to getvariable "LOCAL__Path2" instance kmInst
set Path3 to getvariable "LOCAL__Path3" instance kmInst
end tell
set MultipleFiles to {POSIX file Path1, POSIX file Path2, POSIX file Path3}
tell application "Finder" to reveal MultipleFiles
tell application "Finder"
activate
end tell
AppleScript treats lists as record where every item is quoted, every quoted item is is separated by a comma and the whole thing is then surrounded by curly braces.
You’ll need to convert every list item to exact this kind of thing before putting it into AppleScript…
Thanks for replying. Yes, I tried that. But I just couldn't get anything in a form that AppleScript could use as a list of files to reveal. It worked great with a single file - just not with a list. I'm still sure there must be a way in AppleScript...
You are right - there is a way but I think it kind of depends on how you’ve set up your folders … this is kind of quirky UI Scripting in the Finder that is too much for my tired brain
Will look into this tomorrow.. maybe I can help you with that
but I make no promises
I now have to go to sleep 23 hours awake with nearly 20 of them in front of my Mac is quite enough.
Expand disclosure triangle to view AppleScript source
use framework "Foundation"
use scripting additions
on run
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set pathLines to paragraphs of ¬
(getvariable "local_Paths" instance (system attribute "KMINSTANCE"))
end tell
set xs to {}
tell current application
repeat with fp in pathLines
set end of xs to ¬
(my unwrap(my wrap(fp)'s stringByStandardizingPath)) as POSIX file
end repeat
end tell
tell application "Finder"
set selection to xs
end tell
end run
------------------------- GENERIC ------------------------
-- wrap :: AS value -> NSObject
on wrap(v)
set ca to current application
ca's (NSArray's arrayWithObject:v)'s objectAtIndex:0
end wrap
-- unwrap :: NSObject -> AS value
on unwrap(objCValue)
if objCValue is missing value then
return missing value
else
set ca to current application
return item 1 of ((ca's NSArray's arrayWithObject:objCValue) as list)
end if
end unwrap
property thePath : "Macintosh HD:AppleScripts:"
tell application "Finder"
activate
select {¬
alias (thePath & "1.pdf"), ¬
alias (thePath & "2.pdf"), ¬
alias (thePath & "3.pdf") ¬
}
open selection
end tell
Maybe this could work today as well … this is not tested … since you want it to have a dynamic length of the list - this is what it needs to look like (without the open selection of course ) - you will have to generate this kind of code using AppleScript with the quoted form syntax and compile into a file or build something like a routine into your Script that maybe builds a dynamically changing Script Object which you can run afterwards … but that is not a simple task …
Maybe this is the reason why I haven’t done that by myself until today - I wanted to do that but I didn’t…
Three things to mention:
I am not sure if this code would work today (can not test until Friday because I am not at home)
Ray Barber posted another snippet in the same post that maybe gives you another way of using this kind of code
it only works in one folder but maybe - with additional love put into it and combination with the other snippet I mentioned it could be adapted to use with as many folders you like - but there is one caveat if you happen to have folders configured in list view mode using that one setting that shows the little triangle on the left side of Subfolders be sure that they are not revealing their contents. In this case you maybe will have to deal with the indexes of the rows in the window Layout of the Finder
Thank you very much for this. I will see where it leads!
For the moment I have two solutions provided in the posts above that I can make into Keyboard Maestro Subroutines to be called when I need to reveal several files in Finder.