This is odd. I have script run from script menus that will take a POSIX path in this format:
~/Documents/FolderName/fileName.txt
It show’s a dialog for the user to confirm then reveals the file/directory if it’s present, or creates the directory path and the file if it doesn’t exist.
This was working fine for the last few days, then suddenly it just started showing the fileName and extension “fileName.txt” which, of course, caused the script to fail.
I replaced the display dialog command with DialogToolKit Plus and that worked as intended.
Has anyone else had something similar?
Below is the script. Note, that if the directory is only one or two folders deep display dialog seems to work but if there are multiple folders that’s when it fails.
This version has the display dialog command, which shows the display dialog command then the dialog toolkit command.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "Dialog Toolkit Plus"
(*
------------------------------------------------------------
Script: Create File or Directory from POSIX Path
Author: AppleScript Automation Engineer GPT
------------------------------------------------------------
Purpose:
- Prompt user for a POSIX path (e.g. ~/Documents/test.txt or ~/Desktop/Folder/)
- Expand ~ to home directory
- Create directory or file depending on path type
- Avoid overwriting existing items
------------------------------------------------------------
*)
-- === CONFIGURATION ===
property DRY_RUN : false -- set to true for no-write mode
-- === MAIN EXECUTION ===
try
log "Stage 1: Prompting for POSIX path"
set userInput to text returned of (display dialog "Enter a POSIX path (use ~ for home):" default answer "")
set userInput to GetFileFolderPath()
if userInput is "" then error "No path provided."
-- Expand ~ to actual home directory
log "Stage 2: Expanding tilde"
set shellExpandCmd to "eval echo " & quoted form of userInput
set expandedPath to do shell script shellExpandCmd
log "Expanded path: " & expandedPath
-- Determine if path is a folder or file based on trailing slash
if expandedPath ends with "/" then
set isFolder to true
else
set isFolder to false
end if
-- Check if item already exists
log "Stage 3: Checking existence"
tell application "System Events"
set itemExists to exists disk item expandedPath
end tell
if not itemExists then
if DRY_RUN then
display dialog "DRY RUN: Would create " & (isFolder as string) & " at " & expandedPath
return
end if
log "Stage 4: Creating item"
if isFolder then
do shell script "mkdir -p " & quoted form of expandedPath
else
set dirCmd to "mkdir -p " & quoted form of (do shell script "dirname " & quoted form of expandedPath)
do shell script dirCmd
do shell script "touch " & quoted form of expandedPath
end if
end if
tell application "Finder"
reveal (POSIX file expandedPath as alias)
activate
if not isFolder then
--display dialog
set buttonList to {¬
("XCode"), ¬
("BBEdit"), ¬
("Exit") ¬
}
set userChoice to display dialog ("Open XCode, BBEdit, or not?") ¬
with title ("Done") ¬
buttons buttonList ¬
default button 2 ¬
cancel button 3 ¬
giving up after 30
end if
if button returned of userChoice is "BBEdit" then
tell application "BBEdit"
open expandedPath
end tell
end if
end tell
on error errMsg number errNum
display alert "⚠️ Error (" & errNum & "): " & errMsg
log "Error (" & errNum & "): " & errMsg
end try
on GetFileFolderPath()
set accViewControls to {}
set controlResults to {}
set accViewWidth to 400
set accViewHeight to 200
set theBottom to 0
set spacer to 12
set theTop to spacer
--Variables
set buttonList to ¬
{¬
("Cancel"), ¬
("Okay") ¬
}
set alertText to "Enter a POSIX path (use ~ for home):"
set initialPosition to {30, 30}
-->ACC views go here
--<<Labeled field
set theBottom to (theTop + spacer)
set textInField to "~/Documents/TestFile.text"
set labelText to "File or folder path"
set {theLabeledField, labelFieldLabel, theTop} to ¬
create top labeled field textInField ¬
left inset 0 ¬
bottom theBottom ¬
field width accViewWidth ¬
extra height spacer ¬
accepts linebreak and tab true ¬
label text labelText
set the beginning of accViewControls to theLabeledField
set the end of accViewControls to labelFieldLabel
-->>Labeled field
--<<end ACC views
--Display extended alert
set {userResponse, suppression, controlValues} to ¬
display enhanced alert alertText ¬
message alertText ¬
as informational alert ¬
buttons buttonList ¬
suppression false ¬
acc view width accViewWidth ¬
acc view height theTop ¬
acc view controls accViewControls ¬
giving up after 60
return item 1 of controlValues
end GetFileFolderPath