AppleScripting Default Folder

Hey Folks,

If you have the very useful utility Default Folder you can pull even more tricks than you think.

These are all my AppleScript handlers for Default Folder. They should all be up-to-date for Default Folder X 5.2.2.

I'm not going to bother to explain them – they should be pretty obvious to anyone who has some familiarity with AppleScript – but I will answer questions.

-Chris


EDIT: 2018/05/12 00:07 CDT
           + Added a handler to return the currently selected item in an Open or Save Dialog.


----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/03/14 22:18
# dMod: 2018/05/12 00:06
# Appl: Default Folder X
# Task: Working with Default Folder
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Default_Folder_X, @Handlers
----------------------------------------------------------------
--» Default Folder Handlers
----------------------------------------------------------------
--» defaultFolder_DialogIsOpen()
----------------------------------------------------------------

on defaultFolder_DialogIsOpen()
   tell application "Default Folder X"
      if (IsDialogOpen) = true then
         return true
      else
         return false
      end if
   end tell
end defaultFolder_DialogIsOpen

----------------------------------------------------------------
--» defaultFolder_SwitchToFolder()
----------------------------------------------------------------

on defaultFolder_SwitchToFolder(folderAlias)
   tell application "Default Folder X"
      SwitchToFolder folderAlias
   end tell
end defaultFolder_SwitchToFolder

----------------------------------------------------------------
--» getDefaultFolderCurrentFolder()
----------------------------------------------------------------

on getDefaultFolderCurrentFolder()
   tell application "Default Folder X"
      return (get GetCurrentFolder)
   end tell
end getDefaultFolderCurrentFolder

----------------------------------------------------------------
--» getDefaultFolderSaveName()
----------------------------------------------------------------

on getDefaultFolderSaveName()
   tell application "Default Folder X"
      return (get GetSaveName)
   end tell
end getDefaultFolderSaveName

----------------------------------------------------------------
--» setDefaultFolderSaveName()
----------------------------------------------------------------

on setDefaultFolderSaveName(newSaveName)
   tell application "Default Folder X"
      SetSaveName newSaveName
   end tell
end setDefaultFolderSaveName

----------------------------------------------------------------
--» defaultfolder_GetCurrentSelection()
----------------------------------------------------------------
--  Get the currenly selected item in an Open or Save dialog.
----------------------------------------------------------------

on defaultfolder_GetCurrentSelection()
	tell application "Default Folder X"
		if IsDialogOpen then
			set resultFile to GetCurrentSelection
		else
			error "Dialog is NOT open!"
		end if
	end tell
end defaultfolder_GetCurrentSelection

----------------------------------------------------------------

2 Likes

Nice; thanks! I think I have a couple target uses/abuses.

1 Like

I am trying to implement one that gets the precise file selected - I see you use ":" instead of "/" for directory paths - is that a difference in how they are expressed in Default Folder X versus the Finder?

Anyway, I can get to the folder, but it won't select the file.

Hey Bill,

I've added a handler to the script in post #1 to do this.

The Finder does not express paths with forward slashes. It has it's own reference system.

You can coerce Finder references to alias or to string (and a few other things).

What underlies Unix paths in the HFS file file system are HFS Paths, which as you observed are delimited with colons instead of slashes.

This is not what you're saying above – you're getting NOT setting.

Why specifically do you want to SET the selected file?

Is this in the Open or Save dialog?

Once I know these things I may have some ideas.

-Chris

Chris,

The goal is to select the actual file from that specific folder. I have multiple versions of same-named files in other places, but the files from this folder are the ones optimized for web. The script I have built will insert a token (or "global" in AppleScript-speak) via setting that token to the system clipboard before the AppleScript for the selection of the file gets called by KM. I assume that's the best way to pass something from KM to AS; I may be wrong. Anyhow, I'm hoping there's a way to articulate the full path to the one file under consideration, have it chosen, and then the rest I've got down pat. Then I can loop this X number of times and have the KM script upload these images unattended.

As far as getting or setting, I admit being sloppy in articulating that. It's probably just that I'm not as experienced in AS (haven't used it for a long time). I hope the explanation above also clarifies to you which word I should be using.

Thanks!

Hey Bill,

What app are you using to do the upload?

Will it let you select more than 1 file in the open dialog?

-Chris

Safari itself. The browse button is in Safari, and the upload button is as well. It's the IN-BETWEEN task of selecting the precise file from the precise directory that I cannot master.

It will ALWAYS be just ONE file.

If I understand you correctly, all you need for your macro are the Actions to choose a specified file, correct?

AFAIK, this can't be done using Default Folder X. I don't see any commands to choose a file while in the open dialog. There is a command, SetSavedName and SwitchToFolder that will work for SAVING files, but none for choosing files.

So, here is a solution, somewhat of a kludge, that uses the standard macOS shortcut ⌘⇧G to go to a location (folder), and then a KM Insert by Typing to select the file in that folder to choose. I'd think there is a better way, but I don't see it. :wink:

You will need to adapt this for your needs, and insert the Actions into your current Macro.
Please let us know if this works for you and if you have questions, issues, and/or suggestions.


MACRO:   Using Finder Go To Location Shortcut in File Open Dialog


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/4/d/4da074ab5bcb3453291b8960b8210d51c5a23117.kmmacros">Using Finder Go To Location Shortcut in File Open Dialog.kmmacros</a> (5.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



![image|534x1473](upload://gkSCqcv6RQeRucgf7RphBOrecJk.jpg)

Thanks I will try it out!