how to get Eclipse and Java to play on Yosemite

eclipse_by_a4size_ska-d2dvgyj

If you’re trying to launch the Eclipse IDE with Apple’s old 1.6 JDK installed and find that you’re having trouble updating your Java installation, try the following procedure.

1. Remove Apple’s JRE
First up, let’s get rid of the end user plug in. In Terminal, do

sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin

2. Remove Apple’s JDK
Next, we need to uninstall the Java Development Kit. Do not mess about in the System’s Frameworks/JavaVM.framework folders. You’ll need those. Rather, in Terminal do

sudo rm -rf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk

If you’ve got later versions of the JDK (like 1.7), change the file name in the above command appropriately.

3. Install Oracle’s JDK for Mac
Next go to the Oracle Java page and look for the latest JDK. It’s important that you get the JDK for developers and NOT the JRE for end-users if you want to use Eclipse.

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Download and run the installer. You should now be able to launch Eclipse without problems. :-)





Picture: Eclipse by A4size-ska

how to use AppleScript to help you use AppleScript!

AppleScript search tool


Learning AppleScript can be frustrating. You need a good book, lots of patience, and core documentation like the AppleScript Language Guide and the annual release notes, but most of all you’re going to need access to instant advice. It’s in this last respect that we’re going to build a helpful little AppleScript tool to help us solve AppleScript problems.

The two best places to get help from are Apple’s AppleScript mail list and MacScripter. So, basically, we’re going to combine a couple of tools that you’ve already got (or can get for free) on your mac into a single keystroke-activated, dedicated search engine. When finished, we’ll be able to do something as simple as pressing cmd-ctl-S, type in a short search term like “display dialog” and get specific results for AppleScript.

Our tool basically relies on that fact that in Google you can do site specific searches by using the site: keyword. We’ll then add a couple of AppleScript-specific choices and use a free tool, Red Sweater’s FastScripts, to allow us to assign an easy shortcut (you could assign the keyboard shortcut without FastScripts using Mac’s Services menu, but FastScripts is a great tool you should have anyway if you’re using AppleScript, so now’s a good time to go get it!).

To get started, let’s open Script Editor and start a new script. Our script is really short, and not very complicated, here it is:


set theChoices to display dialog "Search for what?" default answer "" with title "AppleScript Search" buttons {"Cancel", "ASUsersList", "MacScripter"} default button "MacScripter"

set searchTerm to the text returned of the theChoices
set theSite to button returned of theChoices
tell application "Safari"

  activate

  if theSite = "ASUsersList" then
   search the web for "site:http://lists.apple.com/archives/applescript-users " & searchTerm
  else
   search the web for "site:http://macscripter.net " & searchTerm
  end if
end tell

Run the script now, and try a couple of searches. I’ve plugged in MacScripter and ASUsers List, you might like to play with a “choose from list” and add stackOverflow, OS X Technologies or any other sites you know of. Not sure how to do that? OK, try searching for “choose from list” with your new tool, and you’ll soon find out how!

Once you’re finished with the script, use FastScripts or Services to create a shortcut. Now, the next time you’re working in Script Editor and get stuck writing a script or keep stalling over some persistent error message, just hit your shortcut and type in an appropriate search term.

Fast and simple! Automation, that’s what it’s all about!

Addendum: If you’re feeling very ambitious and want to combine several sites into one set of search results, Google allows you to set up your own personal search engine for free.


how to easily import images into new Photos.app

scripting Photos



So with 10.10.3 and Apple’s new Photos’s app, people have been asking how to easily import whole folder’s worth of images into it. Here’s a nice little AppleScript that’ll do it for you very easily. Just plonk the code into Script Editor and hit the run button. :)



Click the image below to get the code ꜜ Screen Shot 2015-04-15 at 20.08.31

Here’s the unformatted code that you can cut and paste into Script Editor, or get if from my pastebin here:

--import images from a folder into Photos.app
-- by Applehelwriter 2015 -- requires Yosemite 10.10.3

set importFolder to choose folder

set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to every file of importFolder whose name extension is in extensionsList

if (count of theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
display dialog "Create a new album with name" default answer "Imports"
set albumName to text returned of the result
set timeNow to time string of (current date)
set today to date string of (current date)
set albumName to albumName & " " & timeNow & " " & today
set imageList to {} repeat with i from 1 to number of items in theFiles
set this_item to item i of theFiles as alias
set the end of imageList to this_item
end repeat

tell application "Photos"
activate
delay 2
import imageList into (make new album named albumName) skip check duplicates yes
end tell

end if

Cocoa: How to show a popover under selected text

Screen Shot 2015-03-25 at 20.49.31

Showing a popover underneath a piece of selected text is a trick widely made use of across OSX, not least in Xcode itself, but also in many other apps. In this post we’re going to look at a simple way to achieve this effect.

1. First of all, in IB drag out a Popover and View Controller and drop them in the Document Outline area. Drag a custom view to the canvas, and hook them all up thus:

Popover’s Outlets
delegate –> App Delegate (or class that will control the Popover)

Popover View Controller’s Outlets
view –> Custom View

Click the Popover object in the Outline area, and in the Attributes Inspector, set the Behaviour to Transient.

In the appDelegate.h file, or the .h file of the class that will control your popover, include the PopoverDelegate in the @interface declaration:

@interface AppDelegate : NSObject <NSApplicationDelegate, NSPopoverDelegate>

2. Still in the header file, you need to make sure the window that the NSTextView is in has a View outlet in its header file.

@property (weak) IBOutlet NSView *aView;

For my purposes, I just have a single window in the appDelegate class, so I just created a view property by control-dragging out of the Window’s view object to the header file. You’ll need to switch to the dual-view Assistant editor to do this:

viewProperty

While you’ve got the Assistant editor open, drag out an outlet from the Popover to the .h file and name it ‘popover’. Finally, in the same way create a similar outlet for your TextView.

4. Next, go into the implementation .m file for the appDelegate (or you class). You’ll need an IBAction to trigger the showing of the popover. In my case, I have an ‘Enter’ button the user hits after making a selection attached to a method I called enterSelection:(NSButton *)sender.

In this method, I first get the rect for the user’s selection with:

NSRect rect = [_textView firstRectForCharacterRange:[_textView selectedRange] actualRange:NULL];

That will return a rect in screen coordinates for the selected text. However, I need to convert that into the window’s coordinates with:

NSRect converted = [_window convertRectFromScreen:rect];

Now we’re ready to call the popover by supplying this rect to the first parameter and the View property we created earlier to the second parameter:

[self.popover showRelativeToRect:converted ofView: _aView preferredEdge:NSMinYEdge];

And that’s it. Your popover should show underneath the selected text whenever your method gets called. If you want to see how this is done step by step in Xcode, check out the video:


 

How to track app use with OSXClock

Screen Shot 2015-03-10 at 17.45.36
OSXClock just got a major update, adding a productivity log that helps you to track how much time you spend actively using each app on your mac.

I wrote an ad-hoc AppleScript to do this sometime ago that proved pretty popular, but I wasn’t satisfied with either the code or the interface. OSXClock improves on that by tapping directly into Cocoa’s API and by offering a more attractive display.

OSXClock is currently on offer for only $2.99. Lots more exciting features are planned for future updates, so now’s a good time to get with the program, folks! :)

*OSXClock requires OSX Yosemite

how to view nib files as xml (or not)

Screen Shot 2015-03-02 at 08.19.50



Xcode being the vast IDE that it is, it’s sometimes the simplest things that flummox you. It’s rare that I ever want to look at the XML code for any of my interface files, unless I’m copying one from one project to another or hunting down some forgotten outlet that’s throwing a warning. But when I do, I invariably forget how to get back to IB view.

If that’s you, fortunately it’s easy to return to the Interface Builder view from the source code view. Just right-click (aka ‘Control click’) on your nib file in the project navigator sidebar and choose “Open as…” and “Interface Builder XIB Document”. Unlike myself, you’ll remember that for next time, too (me, I’ll be looking for this post again in six months time! :) ).



Screen Shot 2015-03-02 at 08.25.23


how to sit down safely

osxclock 1

Well, Tim Cook says sitting is the new cancer…and since I don’t envisage myself in the market for an iWatch, but do need to be reminded to get up and take a break from the desk every 60 minutes, I wrote OSXClock.

I also need a clock for displaying the time on a large screen on occasion when conducting timed-based tests. In the past, I’ve used timeanddate.com for this, but it has a couple of disadvantages: first, you have to be connected to the internet; second, it doesn’t have an alarm or timer.

Hence, OSXClock was born, Applehelpwriter’s answer to the Apple Watch :p, and considerably cheaper! :D


Script Editor battle

graph2-01
Here’s a short video showing some of the differences between Apple’s own Script Editor and my DisplayDroid.

If you haven’t got 5 minutes, the highlights include:

DisplayDroid shows result of each line of the script
DisplayDroid offers more informative error messages
DisplayDroid has automatic language detection (between AppleScript and JavaScript)
DisplayDroid allows you to set a breakpoint on any line in your script
DisplayDroid lets you step through the script line by line

DisplayDroid – limited free betas available

displayDroidFlag-01


The first beta of DisplayDroid is, for a limited time, freely available to anyone interested in testing (requires Yosemite, 10.10). DisplayDroid executes an AppleScript or JavaScript whenever you connect or disconnect an external monitor, primarily with the intent of automatically setting up the environment depending on your display status. DisplayDroid includes its own script editor and a number of presets (i.e., built-in scripts) to get beginners started.

DisplayDroid editor Full details of DisplayDroid can be found here: http://sqwarq.com/displaydroid/ To get a free beta copy of the app, please register for the Community forum here: http://displaydroid.proboards.com and I will email you a beta copy of the app in the next few days.

applescript: toggle Notification Centre (Yosemite)

Notifications Center

Ever since Apple introduced Notification Centre, scripting it has been anything but easy.

I’ve seen brutal shell scripts and a variety of GUI scripts that variously work in Mountain Lion and Mavericks to turn ‘Do Not Disturb’ on and off. With Yosemite, Apple made yet another change to the ui process that controls Notification Centre, which means scripts like this one will choke.

If you’re wondering how to simply toggle whether Notification Centre is enabled or not with AppleScript on Yosemite, here’s the trick (whether this will continue to work in 10.11 is anyone’s guess). Enjoy it while it lasts! :)

tell application "System Events"
    tell application process "SystemUIServer"
        try
            if exists menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2 then
                key down option
                click menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2
                key up option
            else
                key down option
                click menu bar item "Notification Center" of menu bar 2
                key up option
            end if
        on error
            key up option
        end try
    end tell
end tell


%d bloggers like this: