Monthly Archives: December 2015

how to create your own AppleScript commands

Screen Shot 2015-12-25 at 09.53.30



It being Christmas an’ all, we thought we’d contribute something special. What’s the one thing every AppleScripter wants for Christmas? Why “the missing command” of course.

You can bug report Apple all you like to add some command like “remove whitespace from a string” or “get substring” without having to mess around with offsets and so on, but there’s nothing better than helping your self. And that’s what we’re going to do today: learn how to add our own AppleScript commands with a bit of help from AppleScript-ObjectiveC and the use of Scripting libraries.

Put on your party hat and get ready to unwrap your gift. We’re going to learn how to add commands so we can write something like the script shown in the screenshot above.

That requires defining 3 new commands for AppleScript:

substring following  — get the rest of a string after a designated substring

substring preceding — get all the string before a designated substring

remove whitespace — remove all the extraneous spaces and new line characters at the beginning and end of a string.

In order to define your own commands (note to the unwary: beware name clashes!), you need to write an SDEF file and include it in a Library’s script bundle.

The steps are nicely laid out in this complete 26-minute video from OSXAutomation:

(The video is interesting in it’s own right as it sounds like its entirely narrated by the one of the mac’s built in voices).

The library and the sdef file that I created are below, but you’ll need to watch the video to figure out how to put it all together.

A couple of ‘gotchas’ to watch out for:

i. the video doesn’t tell you that you need to include the ‘use framework “Foundation”’ line (you don’t on 10.9, but on 10.11 I couldn’t get it to compile otherwise);

ii. make sure the sdef file name, suite name and library script name all match or you might have trouble compiling.

iii. You’re going to need a heads-up on Objective-C and Cocoa if you don’t already know your way around Apple’s main programming language (Swift? Never heard of ya!) and APIs. Best place to get started with that is Shane Stanley’s intro guides to AppleScript-Objective-C.

If you get stuck, post a comment below and we’ll try to walk you past the obstacles. Once you get the bug, you’ll be writing your own commands left, right and centre!

After you’ve watched the video, you can either write your own commands or come back and copy our sdef file and library script.

If tackling AppleScript-ObjectiveC isn’t something you want to take on right now, let us know in the comments what kind of command you really want to add to your script library and we’ll see if we can’t cook up the code for you.

Alternatively, you can browse through some ready-made script libraries full of extra goodies from this collection.

Merry Christmas all, we’ll see you in the New Year!

🙂

Here’s our sdef file:

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE dictionary SYSTEM “file://localhost/System/Library/DTDs/sdef.dtd“>

<dictionary>

<suite name=”ASTU” code=”ASTU” description=”Commands to edit text”>

<command name=”substring preceeding” code=”SQTXSUBB” description=””>

<direct-parameter type=”text” description=”Get the string up to a first occurrence of a substring”/>

<parameter name=”from” code=”FRoM” type=”text” description=”…”/>

<documentation>

<html>

<![CDATA[<p>set s to substring preceeding “world” from “hello world”</p>

<p>–> “hello “</p>]]>

</html>

</documentation>

</command>

<command name=”substring following” code=”SQTXSUBA” description=””>

<direct-parameter type=”text” description=”Get the rest of the string after the first occurence of a substring”/>

<parameter name=”from” code=”FRoM” type=”text” description=”…”/>

<documentation>

<html>

<![CDATA[<p>set s to substring following “hello ” from “hello world”</p><p>–> “world”</p>]]>

</html>

</documentation>

</command>

<command name=”remove whitespace” code=”SQTXRMWS” description=””>

<direct-parameter type=”text” description=”Remove extraneous whitespace and new line characters from beginning and end of a substring”/>

<documentation>

<html>

<![CDATA[<p>set s to remove whitespace ”  hello world  “</p><p>–> “hello world”</p>]]>

</html>

</documentation>

</command>

</suite>

</dictionary>

And here’s our library script, which we called “ASTU” (short for “AppleScript Text Utilities”).

use framework "Foundation"

on substring preceeding aSubstring from aParentString

set the sourceString to current application's NSString's stringWithString:aParentString

set the searchString to current application's NSString's stringWithString:aSubstring

set theRange to sourceString's rangeOfString:searchString

set theRest to sourceString's substringToIndex:(theRange's location)

return (theRest as Unicode text)

end substring preceeding

on substring following aSubstring from aParentString

set theLength to aSubstring's length

set the sourceString to current application's NSString's stringWithString:aParentString

set the searchString to current application's NSString's stringWithString:aSubstring

set theRange to sourceString's rangeOfString:searchString

set theRest to sourceString's substringFromIndex:((theRange's location) + (theLength))

return (theRest as Unicode text)

end substring following

on remove whitespace aString

set the sourceString to current application's NSString's stringWithString:aString

set trimmedString to sourceString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet())

return (trimmedString as Unicode text)

end remove whitespace

news: App Fixer 1.3 released

We’ve just released App Fixer 1.3, available for free download from here.

how to recover Safari from a browser hijack

Screen Shot 2015-12-10 at 13.32.39.png

The quickest way to get out of a persistent popup that won’t go away (unless you do what it demands!) is to quit or force quit* the browser then restart Safari holding down the ‘Shift’ key.

Holding down Shift allows Safari (or any other app) to restart without resuming its last state.

While this is a great, fast way to solve the problem, it can be annoying if you had other tabs open, and you don’t want to loose those too (or any unsaved data they may contain).

Here’s how you get rid of these kinds of Javascript hijacks without losing your other tabs.

1. Go to Terminal and paste this command (it’s all one line):

defaults write com.apple.safari "com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptEnabled" 0; killall Safari

This turns off Javascript and quits Safari.

2. Reopen Safari
You’ll get all your tabs back including the hijacked tab, but the pop up won’t appear, and you can now close the hijacked tab.

3. Go to Safari Preferences and reenable JavaScript in the Security prefs
(alternatively you can do that in Terminal).
Don’t forget this step, or you’ll think the web is broken!

More sophisticated or persistent adware and malware attacks can be mitigated by using apps like my free App Fixer or DetectX.

*You can force quit an app by pressing the following keys in combination on your keyboard <command><option><esc> then choosing the app you want to quit.

Screen Shot 2015-12-10 at 13.39.57.png

news: FastTasks v2.3 update released

FT2 v2.3 is now available from sqwarq.com. The release notes are here.

Enjoy! 🙂

how to see active internet connections

Screen Shot 2015-12-03 at 15.55.56.png

I was playing around with some ways of detecting active network connections to add as a function in one of my apps — didn’t really work out, so far — but as I was prototyping the code in AppleScript I came up with this little ditty which some of you might be able to make use of:

1. Open the Script Editor

2. Paste the code below into it and hit ‘Run’

#start of script

on getConnections()

set theCmd to "lsof +c 0 -i -n | grep -i established | cut -d \" \" -f 1 | awk '!_[$0]++'"

set theMsg to (do shell script theCmd)

display dialog "The following apps & processes are actively using your internet connection: " & return & return & theMsg with title "Net Tattler" buttons {"Refresh", "OK"} default button "OK"

set theRes to button returned of the result as string

if theRes = "Refresh" then

getConnections()

end if

end getConnections

getConnections()

#eof

If you need more information than just the names of the process, you can play around in Terminal with lsof -iHere’s a great little tutorial.

For something a bit more heavy-duty, check out either Little Snitch or Charles Web Debugging Proxy, both of which are paid-apps but offer free trials. If even those aren’t enough to satisfy your network monitoring desires, head on over to MurusFirewall.com and check out their packet filter GUI offerings for the Mac.

Enjoy 🙂

Acknowledgements

Thanks to the folks over at Etresoft for additional suggestions.

%d bloggers like this: