Skip to content

Instantly share code, notes, and snippets.

@kfriend
Created August 31, 2013 04:16
Show Gist options
  • Save kfriend/6396197 to your computer and use it in GitHub Desktop.
Save kfriend/6396197 to your computer and use it in GitHub Desktop.
AppleScript: Create file w/ prompt
-- Get the currently selected item
tell application "Finder"
set currentFile to selection as text
end tell
set container to GetParentPath(currentFile)
display dialog "File name and extension" default answer "New File.txt"
set fileName to text returned of result
if fileName = "" then
set fileName to "New File.txt"
end if
do shell script "touch \"" & POSIX path of container & fileName & "\""
-- Below grabbed from http://www.nobleswan.com/applescript/AS_snippets.html
on GetParentPath(myPath)
set oldDelimiters to AppleScript's text item delimiters -- always preserve original delimiters
set AppleScript's text item delimiters to {":"}
set pathItems to text items of (myPath as text)
if last item of pathItems is "" then set pathItems to items 1 thru -1 of pathItems -- its a folder
set parentPath to ((reverse of the rest of reverse of pathItems) as string) & ":"
(* The above line works better than the more obvious set parentPath to ((items 1 thru -2 of pathItems) as string) & ":"
because it will not return an error when passed a path for a volume, i.e., "Macintosh HD:", but rather will return ":"
indicating the desktop is the root of the given path. Andy Bachorski <andyb@APPLE.COM> *)
set AppleScript's text item delimiters to oldDelimiters -- always restore original delimiters
return parentPath
end GetParentPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment