@FILES Choose File Name [Example]

###MACRO:   @FILES Choose File Name [Example]

~~~ VER: 1.0    2017-01-14 ~~~

####DOWNLOAD:
@FILES Choose File Name [Example].kmmacros (15 KB)


###Use Case

  • Often we need to enter a File Name (& Folder) to be used in creating a new file.
  • AFAIK, KM does not have a built-in Action to do this.
  • So this macro provides that function, as a building block for your Macros.
  • You will need to use these Actions in your own macro, where you actually create the file.
  • You could also call this Macro as a Sub-Macro.

###ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Get Full POSIX Path to User Entered File Name
  • Provide an example that can be used with other Macros, either by calling this Macro as a Sub-Macro, or embedding these Actions directly.

HOW TO USE:

  1. Trigger macro
  2. Enter File Name
  3. Optionally, Select a different Folder for File

MACRO SETUP:

  1. Change "SET Default Folder to File" Action to the folder of your choice
  2. Change "SET Required File Extension" to your file extenson
    (leave blank for any)
  3. Move this Macro to a Macro Group of your choosing
  4. Assign a Trigger to this Macro
  5. If you do NOT want to be prompted to Confirm/Change the Variables set in the first two Actions, you can DISABLE the "PROMPT" Action.

TAGS: @Files @ChooseFile @AppleScript

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user
  • This macro uses Google Search and Google Chrome, but can be easily changed

ACTION COLOR CODES

  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Varibles or Clipboards

REQUIRES:
(1) Keyboard Maestro Ver 7.2.1+
(2) Yosemite (10.10.5)+



###AppleScript

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property ptyScriptName : "Choose File Name"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2017-01-14"
property ptyScriptAuthor : "@JMichaelTX"

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Get the File Name Entered by User

RETURNS:  POSIX Path of File Name Entered by User

      • IF Canceled by User, returns "[USER_CANCELED]"
      • IF Script Error, returns "[ERROR]" & Error Message

KM VARIABLES REQUIRED: (must be set before calling this script)

  • SCPT__DefaultFolder  -- POSIX Path to Default Folder for Choose file
    • Default: "~/Documents"
  • SCPT__RequiredFileExt  -- Required File Extension
    • Default:  ""  (none)
  
KM VARIABLES SET: (by this script)
  • NONE
  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
*)
set returnResults to "TBD"

try
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  --- SET DEFAULTS ---
  
  tell application "Keyboard Maestro Engine"
    set defaultFolder to getvariable ("SCPT__DefaultFolder")
    set reqdExt to getvariable ("SCPT__RequiredFileExt")
  end tell
  
  if (defaultFolder = "") then
    set defaultFolder to (POSIX path of (path to documents folder))
  else if defaultFolder starts with "~/" then -- Convert tilde to full path
    set defaultFolder to (POSIX path of (path to home folder as text)) & text 3 thru -1 of defaultFolder
  end if
  
  if (reqdExt = "") then
    set defaultFileName to ""
  else
    set reqdExt to "." & reqdExt
    set defaultFileName to "TypeYourFileNameHere" & reqdExt
  end if
  
  --- PROMPT USER TO ENTER FILE NAME AND SET FOLDER ---
  
  set frontApp to path to frontmost application as text
  tell application frontApp
    set filePosixPath to POSIX path of (choose file name with prompt ¬
      "Enter File Name for " & reqdExt & " File to Be Created" default name defaultFileName ¬
      default location defaultFolder)
  end tell
  
  --- ADD REQUIRED FILE EXTENSION IF NOT FOUND ---
  if ((reqdExt ≠ "") and (text -4 thru -1 of filePosixPath) ≠ reqdExt) then
    set filePosixPath to filePosixPath & reqdExt
  end if
  
  set returnResults to filePosixPath
  
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
on error errMsg number errNum
  
  if errNum = -128 then ## User Canceled
    set returnResults to "[USER_CANCELED]" & return & return ¬
      & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer
  else
    set returnResults to "[ERROR]" & return & return ¬
      & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
      & "Error Number: " & errNum & return ¬
      & errMsg
  end if
  
  
end try
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- RETURN THE RESULTS TO THE KM EXECUTE SCRIPT ACTION ---
return returnResults