Category Archives: Technology

AppleScript – Executing JavaScript in Safari and Chrome

I haven’t used AppleScript much, but I was recently working on a script to automate a workflow and I was surprised to discover that each browser uses different commands to execute JavaScript.

Specifically, Safari uses do JavaScript "add code here" in document X and Chrome requires execute javascript "add code here"

Here’s an example of each:

Safari

tell application "Safari"
	open location "https://google.com"
	if (do JavaScript "document.readyState" in document 1) is "complete" then set pageLoaded to true
	display dialog pageLoaded as string
end tell

Google

tell application "Google Chrome"
     open location "https://google.com"
     if (execute javascript "document.readyState") is "complete" then set pageLoaded to true
     display dialog pageLoaded as string
end tell

A couple notes:

  1. AppleScript.app autocorrects the second example to a lowercase JavaScript, but either should work.
  2. Safari requires that a document is specified
  3. If you’re using Firefox, it’s currently not possible to execute JavaScript with AppleScript

Chrome Redirect Cache

I recently created a new site, but I visited the domain before the site and was redirected to a cgi default page. And as a result Chrome cached the URL as http://example.com/cgi-sys/defaultwebpage.cgi

After I got the site up and running, Chrome continued to redirect the domains to the cgi default page even though it was working properly in other browsers and an incognito Chrome window.

After a week or so of this annoyance, I searched the web for a solution and I found one on Sal Ferrarello’s site. It turns out that Chrome caches redirects and there isn’t a clear way to clear the redirect cache.

The solution that worked for me was Sal’s first idea to disable the cache from the Chrome Developer Tools. The easiest way to open the Developer Tools is Command Option I

Once the Developer Tools are open, click on the settings icon shown here:

Once the settings modal opens, check the box next to Disable Cache (while DevTools is open) option as shown here:

disable-cache

Now visit the page that is cached and the old redirect cache should be permanently removed.

Check out Sal’s post for more details.

Karma Mobile WiFi

My brother-in-law turned me onto a WiFi hotspot device called Karma that’s being released in December. I work on the road a few weeks a year and if Karma works as advertised, I’ll be able to work in my car and I won’t have to rely on abysmally slow hotel/airport/cafe WiFi networks.

Karma previously sold a WiFi hotspot, but it didn’t run on LTE networks. Their new device is set to be shipped in December and can be pre-ordered now. If you pre-order one today you can get it for $99 instead of $149. And if you use this link you’ll get an additional $10 off (and I’ll get $10 of data credits):

https://yourkarma.com/invite/kevin23643

Other Details
– No contract
– Pay as you go data – $14/GB
– Runs over the Sprint LTE and CDMA network (view coverage)
– Your Karma connection is shared with other Karma users
– If other Karma users use your connection you get data credits

Karma kind of downplays the security risk of sharing a network connection, which concerns me a bit. Here’s a related response from one of Karma’s employees:

In order to minimize the security risk, I’ll just use a proxy or VPN whenever I’m connected to a Karma device.

I’m really looking forward to using it and I’ll add a review once I get a chance to use it.

Removing Game Center from OS X Yosemite

October 16, 2015 Update: If you’re running OS X 10.11 (El Capitan), you’ll need to disable the System Integrity Protection functionality if you want to hide or remove Game Center.

One of the first things I noticed when looking around OS X 10.10 (Yosemite) was the Game Center icon in the Applications folder.

Game Center has been in Mac OS X since 10.8 (Mountain Lion), but the icon recently changed to match the bubbly iOS icon:

Screen Shot 2014-10-20 at 10.30.59 PM

I never really use Game Center on iOS and am always annoyed with the spammy friend requests. I’ve turned off Game Center friend requests multiple times, but it always seems to get turned back on.

Once I saw Game Center in Yosemite, I immediately tried to drag the icon to the Trash. No luck:

Screen Shot 2014-10-16 at 9.08.37 PM

It took a couple minutes, but I figured out how to remove it. Here’s how your remove Game Center:

Warning! Don’t do this unless you’re comfortable with the command line and have a recent backup of your hard drive. If you don’t know what sudo means, then please don’t proceed. A simple mistake with sudo rm could cause major issues.

– Open Terminal
– Navigate to the Applications folder
– Enter the following:

sudo rm -rf "Game Center.app"

That’s it!

After removing Game Center, I restarted my computer. Game Center didn’t reappear and everything is seems fine. So far. There are probably some ramifications if you try to play Mac App Store games that support Game Center, but I’m not sure. And now I’m wondering if Game Center will reappear then next time I update OS X. Regardless, it’s nice to be rid of that extraneous icon 🙂

Otherwise, I really like Yosemite. It’s really snappy (on a wiped 2012 MacBook Air).

Learn Python the Hard Way – Lesson 15

Mac Terminal Python Commands

I’m going through Zed Shaw’s online course Learn Python the Hard Way and I ran into a study drill that gave me some trouble.

In lesson 15 the seventh study drill is a little tricky if you’re not familiar with the command line. Instead of opening a Python file, you’re instructed to enter the commands from the Python prompt.

Just running open(example.txt) may not work because the Python interpreter only looks in the current folder to find the file. If you don’t start Python in the same folder, you need to tell the interpreter exactly where the file is is located. Here’s one way to do this:

$ python
>>> txt = open("/Users/Kevin/Dev/example.txt")
>>> print txt.read()

If you’re not sure if you’re in the right directory, you can double check your directory. First start Python:

$ python

Then type these two commands:

>>> import os
>>> os.getcwd()

Remember to always import the os module before calling any os commands/functions.

Your result should look like this:

Mac terminal commands to get current directory