Hey guys,
I have a folder full of files: how do I select the first file in finder (and define it as variable - that I can probably do) ?
Thank you!
Hey guys,
I have a folder full of files: how do I select the first file in finder (and define it as variable - that I can probably do) ?
Thank you!
What’s your definition of “first”? Based on the Finder sort order? Hopefully not.
How do you want to select the folder? Open in the Finder?
Hi Peter Lewis,
It took me some days to read other threads on my issue - I just need to select one JPG file from the list, based on date or any other condition.
Then I plan to define path to that file as variable and from there I pretty much now how to proceed.
Thank you
Hey Costa,
You’re not clearly defining what you want to do.
Starting with a given folder path you want to:
A) Get any file?
B) Get a specific file by date?
Be specific and precise and describe what you want step-by-step.
-Chris
Hi Chris,
sorry for being vague.
1 - get any file(JPG) from the defined folder (example folder: Pictures, contains a bunch of JPG files)
2 - set variable with a Path to this file
Thank you!
Hey Costa,
Okay, that’s easy enough with AppleScript:
set sourceFolderPath to "~/Downloads/"
tell application "System Events"
set theFilePath to POSIX path of (first file of disk item sourceFolderPath whose visible is true)
end tell
return theFilePath
Alternatively you could use a Keyboard Maestro For Each with Path in a Folder and Break from Loop after the first item is found. That’ll get the path of the first item in the folder into a variable.
-Chris
Looking for a way to replace "~/Downloads" with a Keyboard Maestro variable, I stumbled a bit (I have no idea how to program) and tried the following which — bedazzled! — works. Thanks much!
(Posted as an image because I couldn't get the helpfully colored formatting to paste into the forum editor.)
The reason for the variable-to-get name "ParentNameFull" is because I've set up folder renaming macros to expect that I have expanded the folders and selected one file in each folder before the macro is run. I extract information from the files and apply it to the folder names.
This bit of AppleScript will allow me to select folders, get inf from a file inside each folder, and apply it to the folder itself. (The source variable name will be "FileNameFull" — a/k/a "path" — not "ParentNameFull" {I always founder on my indecision whether "path" includes the file name or is just the path to the file}. The returned variable will be "ChildNameFull".)
A little help with my human parsing of the AppleScript. Where does the variable "set" by Keyboard Maestro Engine live, and for how long?
A KM Variable is global and permanent by default, unless you give it a prefix of “Local” or “Instance”. They live in a KM plist file. See Variables (KM Wiki).
I know about the "internal" Keyboard Maestro variables (in my example, “ParentNameFull”). I was wondering about the AppleScript variable "sourceFolderPath". Once it is "set" by the AppleScript command, does it live outside of memory? What happens to it when the AppleScript finishes?
Is there any reason to not use the same name for the variables whose values are being shunted back and forth? Such as:
I'm sure this is both dead-simple beginner's stuff, and is outside the remit of this forum — so please do not feel obliged in even a faint way to continue. But know that I appreciate your teaching.
AppleScript variables die when the script ends.
I’m sure Chris will have some suggestions, but here are mine:
Learning AppleScript - MacOSXAutomation.com
Script Editor Overview
Mac Automation Scripting Guide – AppleScript & JXA – Apple
AppleScript Language Guide
Unless the script resides in a .scpt file and the variable is defined as a property, in which case the value of the variable in the script lives on.
See properties here:
Note that the script has to be an a compiled script file of some sort (like scpt), not text within a Keyboard Maestro action, nor a .txt or other text only AppleScript file formats.
Yep, I am well aware of that AppleScript properties are retained after the script end, but it seems like an advanced, edge case for most KM users.
Plus, most scripts used in KM that I have seen are entered as text in the KM Execute Script Action, in which case the script properties are NOT remembered.
Also, based on Apple’s reference, properties and variables are technically two different entities. So I think I will stand by my statement: