A questions about applescript paths

Hi,

My Applescript looks like this and works well:

tell application "Keyboard Maestro Engine"
	set Chesspgn to getvariable "Chess Pgn"
	set pgnfilename to getvariable "pgnfilename"
end tell
tell application "Keyboard Maestro Engine"
	say Chesspgn using "Kate" saving to (((path to desktop) as string) & pgnfilename)
	--speaking rate 140 
end tell

But for the life of me I can’t make it write to
“/Users/myusername/Dropbox/ChessPGNs”

Any tips would be appreciated. I have read numerous webpages on the topic but still can’t do it! I’m fairly sure I need to set the path using POSIX and an alias. I’ve tried many many variations but all result in error messages:
Keyboard Maestro Engine got an error: Can’t make “/Users/username/Dropbox/ChessPGNs” into type constant.

(Full disclosure, I’ve written this request on the KM forum as well, wherever I get the first answer, I’ll update the other one. If that’s bad of me, feel free to delete, or let me know and I will).

I can tell you that this works for me:

set spoken to "Hello"
set fileName to "hello.aiff"
set filePath to (path to desktop as string) & fileName
say spoken using "Alex" saving to filePath

So my guess is that there’s something funky in your Keyboard Maestro variables. Can you tell us what “Chess Pgn” and “pgnfilename” are set to in KM?

Side notes that may have nothing to do with the error:

  1. I don’t understand why you’re expecting a file to be created in your Dropbox folder when you’re telling it to be saved to your Desktop.
  2. The (path to desktop) as string construction results in a colon-separated path, e.g., Macintosh HD:Users:username:Desktop:, not a POSIX style slash-separated path. The say command is perfectly happy with the colon-separated form. The Apple docs say you can use an alias as the saving to argument, but that works only if the file already exists.
  3. There’s no need to put the say command inside a tell block. It’s built into AppleScript, not part of the KM Engine dictionary.

Hi drdang,
Thanks for your reply.
In KB, Chess Pgn is a variable containing text, and pgnfilename is a generated variable that ends up being a filename like “Carlsen vs Caruana 2019.aiff”

Sorry I wasn’t clearer about my question, and to answer your side notes:

  1. I don’t understand why you’re expecting a file to be created in your Dropbox folder when you’re telling it to be saved to your Desktop.

The macro works as is, and saves a file to the desktop, therefore telling me that the rest of the macro works, but I can’t calculate the formatting to get it to save to a different folder. When I try to modify it to a different folder, it stops working.

  1. There’s no need to put the say command inside a tell block. It’s built into AppleScript, not part of the KM Engine dictionary.

Aah. Thanks!

Now this did the trick! It still took a few trys but here’s the working script:

tell application "Keyboard Maestro Engine"
	set Chesspgn to getvariable "Chess Pgn"
	set pgnfilename to getvariable "pgnfilename"
end tell

	set filePath to "Macintosh HD:Users:username:Dropbox:ChessPGNs:" & pgnfilename
	say Chesspgn using "Kate" saving to filePath
	--speaking rate 140 

Thank you so much!

Now I see how I misread your question. Should have waited until I had my first tea of the day. Luckily, I babbled long enough to include some useful information.

Although I don’t think this will make your script any better, there is a way to get the path to the “ChessPGNs” folder in your “Dropbox” folder without including your username or the name of your hard disk in the script:

set filePath to (path to home folder as string) & "Dropbox:ChessPGNs:" & pgnfilename

This can be useful to know when you want to share a script with someone else.

And just to give the say command its due, it can use POSIX paths, but the scripting gets even messier:

set filePath to POSIX path of (path to home folder as string) & "Dropbox/ChessPGNs/" & pgnfilename

Hi!

Oh that’s fantastic news, as I’ve made the full script available as a macro on the KB site, and would like to be able to update it easily, and having to change the username is the thing that slows that down the most. Many thanks!

I’m actually very pleased with the full project now, when I .pgn (a chess game) file is put in a dropbox folder, KM turns it into a dictated audio file, by regex the notation into english. So 1.e4 e5 turns into, “Move 1, (little pause) White, pawn to e4, black pawn to e5” etc.

Getting the script to write to the dropbox folder was almost the fine step. Only fine tuning now. Thanks again.