How to generate a text file based on the name of a folder

atfon

macrumors newbie
Original poster
Feb 26, 2015
4
0
Here is what I am trying to accomplish with an AppleScript:

Scenario: I have a Folder A anywhere on my computer. In that folder is folder B. I would like to run an AppleScript script in folder A that will generate a text file with the same name as folder B and for that text file to be created in folder A.

• Folder A can be anywhere on the computer.
• Folder B can have any name and can include spaces.
• Folder B will have text after a period at the end of the name. I would like to replace this with .txt.

Ideally, it would be nice if the script could check if the text file in Folder A already exists before creating it.

I would like to be able to run this script in any folder on a computer and for it to successfully generate the resulting text file based on the contained folder.
 

atfon

macrumors newbie
Original poster
Feb 26, 2015
4
0
As you may be able to tell, I am a newbie when it comes to AppleScripting. I have tried a few things, but they have been frustratingly untenable. I was able to create a bash script that works, but it does not work through the Automator.

I am happy to hear this is achievable by AppleScript because that is my preference and I am stumped. Any guidance would be much appreciated.
 

superscape

macrumors 6502a
Feb 12, 2008
920
214
East Riding of Yorkshire, UK
Okay, I'll point you in the right direction.

Take a look at how to create services in Automator. There are plenty of tutorials online e.g. http://arstechnica.com/apple/2011/03/howto-build-mac-os-x-services-with-automator-and-shell-scripting/

I'd be tempted to create an Automator service that executes either a shell script or an AppleScript. If you already have a shell script working then it will probably take only a little tweaking to make that work.

Saving the Automator workflow as a service means that when you've finished it, you can run it by control clicking the folder of your choosing in the Finder.

Have a play, try a few things and come back and ask if you run into problems.

Good luck!

----------

Ah, what the heck, because it's Friday and I'm in a good mood here's a partial solution for an Automator AppleScript:

Code:
on run {input, parameters}
	
	set theFolder to item 1 of input --because 'input' is a list
	
	tell application "Finder"
		set theName to (name of theFolder) & ".txt" --make the name of the text file
		set thePath to POSIX path of (theFolder as alias) & theName --make the path
	end tell
	
	--do something here to make the file. I'll leave you to figure that out for yourself.
	
	return input
end run
I've left out the bit where the file is actually created because I'm not doing *all* the work! ;-)
 
Last edited:

atfon

macrumors newbie
Original poster
Feb 26, 2015
4
0
Thank you for the suggestions. I was trying something very simple like the following that fails with the message that it can't make into type alias:

Code:
set sourceFolder to (choose folder) & ".txt"
set theFile to POSIX path of (sourceFolder as alias)
tell application "Finder"
	make new file of theFile with properties (sourceFolder as text)
end tell
 

superscape

macrumors 6502a
Feb 12, 2008
920
214
East Riding of Yorkshire, UK
Yup. A few problems there. I've only got a few mins to reply, so here are a few pointers:

  • Get rid of the ".txt" from the "choose folder" line - add it to the path later
  • The Finder wants an alias, not a posix path
  • I'd probably use 'touch' in a "do shell script" to create the file
 

Mark FX

macrumors regular
Nov 18, 2011
119
8
West Sussex, UK
There are a dozen different ways to do this with Applescript, but one way is like this.

Code:
try
    set theSourceFolder to (choose folder) as alias -- Get Folder B From User.
on error
    set theSourceFolder to missing value -- User Cancelled the Get Folder Dialog Box, so Set theSourceFolder Variable to nil.
end try
if theSourceFolder exists then -- Check the User Selected a Folder.
    tell application "Finder"
	set theTextFileName to (name of theSourceFolder) & ".txt" as text -- Set the Text File's Name.
	set theParentFolder to container of theSourceFolder as alias -- Get theSourceFolder's Parent Folder, or Your Folder A.
	set theTextFilePath to (theParentFolder as text) & theTextFileName as text -- Set the Complete Path to the Text File.
	try
	    set theTextFile to theTextFilePath as alias -- Check if the Text File Exists Already.
	    return -- Return and Do Nothing if the Text File Already Exists.
	on error
	    try
	        set theTextFile to make new file in folder theParentFolder with properties {name:theTextFileName} -- Create the Text File.
	    on error -- Error Occured While Trying to Create the New text File.
	        display dialog "Error! While trying to create the new " & theTextFileName & " file." buttons {"OK"} -- No Error with only one Button.
	    end try
	end try
    end tell
else
    return
end if
You don't really need to use Automator for this project, a simple double clicked Applescript would suffice.
If you are working on more than one folder, I would probably prefer to choose the Parent Folder A, and then display the child folders in a choose from list dialog box, which you can then act upon, and re display the choose from list dialog again, until the cancel button was clicked to end the script.

Regards Mark
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
8,373
4,361
Pumpkindale
You don't really need to use Automator for this project, a simple double clicked Applescript would suffice.
If you are working on more than one folder, I would probably prefer to choose the Parent Folder A, and then display the child folders in a choose from list dialog box, which you can then act upon, and re display the choose from list dialog again, until the cancel button was clicked to end the script.
I would use Automator to make a Service, which is then available in Finder's right-click contextual menu. That way all I'd have to do is right-click on the folders I wanted to produce txt files for, then choose the service item. This would work even if I selected multiple folders before right-clicking.

Maybe that's just me. Some people prefer "Repeat one item at a time", while others prefer "Apply to all".
 

atfon

macrumors newbie
Original poster
Feb 26, 2015
4
0
Brilliant, Mark. That's exactly what I was looking for. Thank you to you both for all your help. This was a very good learning experience.
 

superscape

macrumors 6502a
Feb 12, 2008
920
214
East Riding of Yorkshire, UK
I would use Automator to make a Service, which is then available in Finder's right-click contextual menu. That way all I'd have to do is right-click on the folders I wanted to produce txt files for, then choose the service item. This would work even if I selected multiple folders before right-clicking.
Yup, that's pretty much the angle I was taking. For the record, I'd have kept it simple and done something like this:
Code:
on run {input, parameters}
	
	set theFolder to item 1 of input --because 'input' is a list
	tell application "Finder"
		set theName to (name of theFolder) & ".txt"
		set thePath to POSIX path of (theFolder as alias) & theName
	end tell
	
	do shell script "touch " & quoted form of thePath
	return input
end run
...within an Automator service, of course.