Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

touchdown100

macrumors member
Original poster
Mar 20, 2014
30
0
I have been trying for a while to create an Applescript that would launch a create file dialog box. I would then enter the file name to create, the location to save to and the Tags to assign.
Everybody online told me a way to do it which involved basically having a script that says if a Finder is open save to the current folder if no window open then save to desktop. Then open a simple dialog box and create file.
I though wished to do it another way. i wanted to have a save file as type box appear whereby you enter the name of the file to create and the location and then assign Tag to it.
I managed to get the first part working but could not get the script to assign a Tag.
I managed to stumble upon the solution by accident so here is my full script program for anyone it may help

Code:
set filename to choose file name with prompt "choose file" default name "untitled.txt" default location (folder of the front window) as alias
open for access filename with write permission
write filename to filename
The code that assigns the Tag(s) is the last part. Write filename to filename

I wrote it thinking it would do something else. However what I think it actually does is take the contents of the variable named filename and writes them to the parameters of the actual file it creates.
The contents being the location to save the file. The location of the file just created is the same so nothing changes, then the name of the file, that too is the same so it is not changed. Lastly the Tag which is not present so is then set to the newly created file.
and hey presto! I think this is not the correct way to assign a tag to a file you create with Applescript but it works and does so with one line of code so I hope you find it useful
 
Last edited by a moderator:
It seems to have an issue with that first part in Yosemite:

Code:
default location (folder of the front window) as alias

When I took that off, it defaulted to my Documents folder and worked fine.
 
I made a mistake

the line of code you reference was part that I altered from the original script. The original line was (path to desktop) I changed it as I wanted to have the default location save to the current open folder.
I messed up, sorry...but then I am new to programming lol
I hope that the script helps and maybe gives ideas on how to take it further.
 
the line of code you reference was part that I altered from the original script. The original line was (path to desktop) I changed it as I wanted to have the default location save to the current open folder.
I messed up, sorry...but then I am new to programming lol
I hope that the script helps and maybe gives ideas on how to take it further.

No worries. I did take out the last line so it just made the file. It's useful at this stage. Set up a keyboard shortcut and profit. :D
 
Cool

Cool, let me know if I can help with anything else and i will see what I can do.
 
Full script

Here is the full code for a script I was working on. I wanted to place an icon on the Finder Window and once clicked it runs, showing a dialog box that gives you 3 choices. Create new file, create new folder or create new alias. then you choose one, say create new file, and it lets you input the file name to create, save location and any tags to assign. The create the file based on those inputs.
Same goes for the other 2 choices. I know there are easier solutions but this is neater and a bit better I think. Anyways I hope this is of use to you. here then is the full code.

Code:
-- Script to let user create a file and save it to location of their choice

-- Created by Touchdown © 2014
-- Can be edited as you see fit as long as you do not change the copyright details and I retain credit

-- Creates a list for the user to choose which action they want to perform

choose from list {"Create New File", "Create New Folder", "Create New Alias"} with title "New Item" with prompt "Please Make Your Selection:" default items "Create New File" OK button name "OK" cancel button name "Cancel" multiple selections allowed "false" empty selection allowed "false"

-- sets variable called listanswer to the result of the selection as text
set listanswer to result as text

-- if the selection equals the first choice then do the following
if listanswer = "Create New File" then
	
	-- Creates a variable called filename and saves into it the choices made by choose file name with prompt command.
	-- Then creates the file using the information contained in filename
	
	set filename to choose file name with prompt "Choose File" default name "Untitled.txt" default location (path to home folder) as alias
	open for access filename with write permission
end if

-- if the selection equals second choice then create new folder based on user choice
if listanswer = "Create New Folder" then
	tell application "Finder"
		set newfolder to text returned of (display dialog "Please Enter Folder Name:" default answer "New Folder")
		set loc to choose folder "Choose Folder Location"
		set newfoldername to newfolder
		set newfo to make new folder at loc with properties {name:newfoldername}
	end tell
end if

-- if the selection equals the third choice then create new alias based on user choice
if listanswer = "Create New Alias" then
	tell application "Finder"
		set newalias to choose file with prompt "Choose File To Create Alias For"
		set loc to choose folder "Choose Folder Location"
		make new alias at loc to newalias
		
	end tell
end if

-- if you wish to add more items to the menu then first add them to the choose from list command at the top of the script. Then add a if listanswer = "your menu item" then (whatever you want to do). Don't forget to end with an end tell statement. Make sure that "your menu item" is typed exactly as it appears in the choose from list command.
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.