Create new file in folder on desktop

I’m trying to create a file in a folder on my desktop using Apple Script.

set blankPage to (((path to desktop) as string) & “text.txt”)

I know path to desktop point to the desktop, but how do I get it to point into a folder on the desktop ex. (“Text files”) ?

You may try :


set subFolder to "Text files"
set blankPage to (((path to desktop) as string) & subFolder & ":text.txt")

Yvan KOENIG (VALLAURIS, France) mercredi 10 septembre 2014 12:06:15

Thanks just what I needed.

First of all Welcome to MacScripter!

You don’t need that many parentheses. When you want to concatenate values Applescript will take the first value into consideration. Here a few examples to make it more clear

"Number " & 1 -- results : string concatenation
{} & "Number " & 1 --results : adding items to list
1 & "Number " --results : item 2 can't be coerced into the same type as item 1 which creates a list of items

So what we’ll need is to put the scripting addition command path to into parentheses so this command is executed before we concatenate the strings.

set subFolder to "Text files"
set blankPage to (path to desktop folder as string) & subFolder & ":text.txt"

Hello.
Here is another way that returns the hfs path to a folder. It utilizes that the desktop is Finders “home folder”.

tell application "Finder" to set mf to (folder "Text files" as text)
log mf

Another subtlety in this case is that the ‘path to’ command happens to have an optional ‘as’ parameter, which can be used to specify a text result. You should be able to see the different colouring of ‘as’ if you compile the following:

(path to desktop as text) -- 'path to' with 'as' parameter.
((path to desktop) as text) -- 'path to' followed by AppleScript coercion.

In the first line, ‘path to’ returns the text directly. In the second, ‘path to’ returns an alias, which is then coerced to text in a separate process. The first is more efficient, but they both work and you’re unlikely to notice the difference in speed.

The reason I mentioned it was not because of performance reasons but In programming using too much parentheses to execute sub parts of code when it’s not needed is bad practice in general. Having 3 parentheses where 1 is definitely not needed and the other is dubious was was I thought worth mentioning.

Sometimes I take time to comment and make suggestions to enhance code but sometimes I’m in a hurry.
It was the case with this thread ” time to lunch is sacred in France ” so I didn’t tried to correct the original syntax.
Just answer the question :
how do I get it to point into a folder on the desktop ex. (“Text files”) ?
using the asker’s code as a starting point.

Yvan KOENIG (VALLAURIS, France) mercredi 10 septembre 2014 18:56:24