How to make AppleScript ignore escape characters in string to make a Note

I have a python script that takes a user’s input and inserts it into the name and body parameter of the make new note apple script. The problem is that any escape or special characters get interpreted by applescript. I want all strings to be treated as raw and to ignore all escape characters and things like :\//tft// without giving me a Expected “"” but found unknown token. error

The line of python looks like this:

cmd = "osascript -e 'tell application \"Notes\" \n tell account \"iCloud\" \n make new note at folder \"Notes\" with properties {name:\"%s\", body:\"%s\"} \n end tell \n end tell'" % (header, body) 

… where header and body are the user supplied strings.

But for manual testing I use the Script Editor to quickly reproduce the error.

tell application "Notes"   tell account "iCloud"     make new note at folder "Notes" with properties {name:"myname", body:"c:\test\test.txt"}   end tell end tell 

This particular note ends up looking like this:

enter image description here

I know that I can use \ instead of \ but I don’t want users to have to sanitize all their input as they could be copy pasting from a large body of text. Think of a log file or paste-bin style body of text.

Is there a way to programmatically sanitize the user’s input?

Node program runs in Terminal, but not as a service or on command “do shell script” from AppleScript

I have a call (mercury-parser via Yarn) that converts Web pages to lines of structured text. It runs in node on 3.5 GHz, 6-Core Mac Pro (MacPro6,1) with 16GB RAM and D700 GPUs, running macOS 10.14.5 very well, but I’m struggling to run it as a service / on demand without opening terminal app and typing.

I would much prefer it to run within an AppleScript. Here is the line of code:


If I run it in Terminal (using either /bin/bash or /bin/sh), I get the proper result: fourteen lines of structured text from the Web page.

If I run it in a shell script, starting with either “#!/bin/sh” or “#!/bin/bash”, I get: env: node: No such file or directory If I run it in Script Editor as: do shell script “/Users/me/.yarn/bin/mercury-parser” I get: env: node: No such file or directory

Here are both my .profile and my .bash_profile texts:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$  PATH"  export PATH="$  HOME/.yarn/bin:$  HOME/.config/yarn/global/node_modules/.bin:$  PATH"  [[ -s $  HOME/.nvm/ ]] && . $  HOME/.nvm/  # This loads NVM  source ~/.nvm/  export NVM_DIR="$  HOME/.nvm" [ -s "$  NVM_DIR/" ] && . "$  NVM_DIR/" # This loads nvm 

What do I have to do to get the code to run outside of Terminal?

Automator and/or AppleScript to automatically Command-F2 to enter Target Display Mode

I’m trying to use Automator with AppleScript to automatically hit Command + F2 to enter Target Display Mode. The script runs but I just get a beep/tone with no action. I don’t know if there’s an error in the script or if it’s not capable of hitting a function key.

on run {input, parameters}      tell application "System Events"         key down {command}         keystroke "120"         key up {command}     end tell return input end run 

Once I get this working I’ll need to somehow schedule this to run on startup or maybe on a schedule. I’ve seen some threads on this but never an actual solution. I’m using a 2011 iMac as a secondary monitor and hate hitting Command + F2 every day.

error with automator applescript “do shell script” call (file not found) yet command line with same command works

I have a simple automator app that runs a simple script. It uses ssh to open a file on the local machine, MachineA, with an app on the remote machine, MachineB, and logs the ssh call.

on run {input, parameters}     repeat with fn in input         set fn to POSIX path of fn         set fn to "/Volumes/MachineA" & fn         set fn to "\"/usr/bin/open -a /Applications/ " & fn & "\""         set cmd to "ssh -x MachineB.local " & quoted form of fn         do shell script "echo " & cmd & " >> ~/cmd.log"         do shell script cmd     end repeat end run 

The log output is:

ssh -x MachineB.local "/usr/bin/open -a /Applications/ /Volumes/MachineA/Users/<USER>/Documents/<FOLDER>/<FILE>.pdf" 

When I use the app to open a file (drag & drop), an error is thrown.

error message

However, if I copy the output in the log file to the command line, success, the file is opened on the remote machine. Why the difference?

AppleScript to clean spam in Messages

I get a lot of spam in my iMessage, such gamble, sex etc.

  • On my iPhone, it can be filtered into a separate column.

  • But on my Mac, there is no way to filter messages from unknown senders, which is annoying.




Is there a way to write an AppleScript to clean spam messages that contain specific words? I can trigger it at a certain interval to clean up spam messages.

AppleScript for Excel to select/copy all cells?

Applescript can tell Excel to select a specific range of cells, e.g.

select (range "A1:B20")

and you can copy cells:

copy (range "A1:B20")

However, how can you tell Excel to select all the cells in a worksheet, or to copy all the cells in a worksheet? It seems to require a specific range. However, of course, when you are working with Excel directly you can just press cmd+A to select all.

Refactoring Inelegant AppleScript to Use Recursion

I’ve written an AppleScript to iterate through an app’s menu choices, doing something with each item (e.g., get its keyboard shortcut). It works, though (being AppleScript) it’s very slow, but I’m unsatisfied with it.

As you can see from the skeleton below, I’ve used a brute force nesting of repeat loops to work down through the menu tree. It gets particularly ugly when the skeleton is fleshed out to do actual work, requiring repetition of instructions so brief that putting them into their own handlers doesn’t really help.

It seems like a recursive algorithm would be a more elegant solution, but I’ve been unable to implement one. I’ve foundered on AppleScript’s lack of dynamic variable assignment.

Is there a more elegant solution?

-- Iterates through an app's menus, performing an action on each item  --list of apps to process set theApps to {"Stickies"}  repeat with thisProc in theApps      --wake up     launch application thisProc      --process this process     tell application "System Events" to tell process thisProc          --iterate through submenus, ignoring anything beyond three levels          --there are always menu bar items         repeat with levelOne in menu bar items 2 through -1 of menu bar 1             Get_Something of me from levelOne             --there are always level 1 menu items contained in a menu 1             repeat with levelTwo in (menu items of menu 1) of levelOne                 Get_Something of me from levelTwo                 --fail silently if there's no level 3                 try                     repeat with levelThree in (menu items of menu 1)                         Get_Something of me from levelThree given indent:3                         --fail silently if there's no level 4                         try                             repeat with levelFour in (menu items of menu 1) of levelThree                                 Get_Something of me from levelFour                             end repeat                         end try                     end repeat                 end try             end repeat         end repeat     end tell end repeat  to Get_Something from thisItem      tell application "System Events" to set theName to title of thisItem      --ignore menu separators     if theName is not missing value and theName is not "" then         say theName     end if  end Get_Something 

Adding hyperlink within message content of new email using AppleScript

I’ve been searching the internet but am having trouble finding a way to attach a hyperlink to a new email using applescript.

The most helpful link I’ve found is from 2008, and I believe the poster was eventually able to add a hyperlink, but only within a mail signature.

I am already using another mail signature and I’m wondering if there is some other way to do this since it’s been 10 years since the original post.

This is an example of my code:

property thesender : “Belinda “

tell application “Microsoft Excel” set lasty to first row index of (get end (last cell of column 1 of sheet 1) direction toward the top) end tell

repeat with i from 2 to lasty

tell application "Microsoft Excel"      set pername to value of column 1 of row i of sheet 1 of active workbook as text      set peremail to value of column 2 of row i of sheet 1 of active workbook as text   end tell   tell application "Mail"      set theAttachmentFile to "Macintosh HD:Users:abc:Desktop:Photos:abc.jpg"      set signatory to "CXO"      set newemail to make new outgoing message with properties {content:"Hi" & space & pername & "," & return & return & "I hope all is well.  My name is Belinda with Company X. We recently published a peer-based Technologies Survey that I thought you would find valuable & wanted to share." & return & return & return & return & "Download your copy of the survey here: Technologies Survey Powered by Company X. 

Please reach out with any questions or if you’d like to learn how our AI-based platform is helping your peers optimize spend.

I look forward to connecting.

Thanks, Belinda”, visible:true, subject:”ABC Survey Results for” & space & pername, message signature:signature signatory}

    tell content of newemail         make new attachment with properties {file name:theAttachmentFile as alias} at after sixth paragraph     end tell      set sender of newemail to thesender      tell newemail          make new to recipient at end of to recipients with properties {name:pername, address:peremail}      end tell  end tell 

end repeat

‘I need the hyperlink to sit within the message content where it says “Download your copy of the survey here.”

Any help would be greatly appreciated!