Reputation: 9575
For some reason Finder on Mac doesn't come with create new file functionality. The fastest way I've found to create a new file is to drag the finder path into Terminal and create a file there... which is super annoying
I wrote an apple script to automatically create a new file and bound it to a shortcut. It works perfectly, except that I can't figure out how to open the new file from within the applescript. I'm pretty sure the error stems from POSIX / UNIX paths but couldn't find a way to get this to work even when I put POSIX next to file, currentDir etc.
Here's my script:
set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set file_name to (text returned of file_name)
tell application "Finder"
set theWin to window 1
set currentDir to POSIX path of (target of theWin as alias)
make new file at (the target of the front window) as alias with properties {name:file_name}
set currentPath to (currentDir & file_name) as string
open file currentPath
end tell
The script creates the file, then errors out saying it can't find the file to open it.
Upvotes: 0
Views: 965
Reputation: 3142
I cleaned up the code a bit and got rid of unnecessary lines.
set fileName to text returned of (display dialog "File Name" default answer ¬
"untitled" with icon note buttons {"Cancel", "Continue"} default button 2)
tell application "Finder" to set newFile to (make new file at Finder window 1 ¬
with properties {name:fileName}) as alias
delay 0.1
do shell script "open -e " & quoted form of POSIX path of newFile
Note: If you're trying to use open -a
instead of open -e
... And you were getting error messages, try using open -b
. This option allows you to open the files with the application by using the application's bundle identifier to identify the application to use.
For example, if I want to open the files with Visual Studio Code.app by using it's bundle identifier... The do shell script
command would look like this...
do shell script "open -b com.microsoft.VSCode " & quoted form of POSIX path of newFile
If you do not know how to get the bundle identifier for the application to use when opening the file, this following AppleScript code allows you to choose an application then copies it's bundle identifier to the clipboard. Then just go back and paste that into the do shell script "open -b "
command.
activate
set chosenApp to name of (choose application with title ¬
"Choose App" with prompt "Choose App")
delay 0.1
tell application chosenApp to set appID to id
set the clipboard to appID
Be sure to grant Terminal.app the appropriate permissions in System Prefs.
Upvotes: 2
Reputation: 1205
If you plan on doing file activities with the Finder it is best to avoid using posix paths. You have a couple of redundancies in your code so I streamlined it a bit.
set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set file_name to (text returned of file_name)
tell application "Finder"
set theWin to window 1
set currentDir to (target of theWin as text)
--> "MacHD:Users:username:Hours of Operation:"
make new file at theWin with properties {name:file_name}
set currentPath to currentDir & file_name
--> "MacHD:Users:username:Hours of Operation:untitled.txt"
open file currentPath
end tell
Try putting this after the above (and commenting out the above open file…
line).
tell application "System Events"
activate application "TextEdit"
open file currentPath of application "TextEdit"
end tell
currentPath
Also, after trying the above, put the variable name currentPath as the last line of the script (as I added above) and run from Script Editor. Its value should appear in the 'Result'. You can replace the drive/username components but I want to see the exact layout of the string.
A couple of things to consider:
Some files can't be opened this way, for example, a jpeg. Preview will balk at opening an empty image, presumably because that's not how images are made. Complex file formats (e.g. pages) will also fail. But with a plain text file, there shouldn't be any issues. As such, I'd consider adding '.txt' to your default filename.
Upvotes: 0