16 Mar

macOS Sierra: Export PDFs from Pages using AppleScript

It appears that macOS Sierra introduces a bug in Pages which means that you get a sandbox/permissions related error when trying to export a PDF using AppleScript.

Hopefully, Apple will fix it soon. However, until then, here’s a workaround (see sample below in a handler suitable for Automator).

The idea is to take advantage of the fact that the Pages file is a package (in effect, a folder) and temporarily save the PDF there, having done that, we move it to its correct location using the Finder. Scruffy, but it works.

on run {input, parameters}

repeat with theFile in input
tell application "Finder"
set theFilesFolder to (folder of theFile) as text
end tell

tell application "Pages"
set theDoc to open theFile

set theDocName to name of theDoc
set theName to (characters 1 thru -7 of theDocName) as text
set thePDFPath to ((theFile as text) & theName & ".pdf") as text
export theDoc to thePDFPath as PDF
close theDoc

end tell

tell application "Finder"
move file thePDFPath to folder theFilesFolder with replacing
end tell

end repeat

end run

While we have your attention… why not check out our macOS apps?

Barcode Basics – macOS barcode generator (including Automator support)
Ai Actions – Automator action pack for Adobe Illustrator
Pages Automator Actions – Automator action pack for Pages

16 Feb

Making shell scripts more user friendly

We were recently asked whether it was possible to copy all the files that contain some variable text from Folder A into Folder B as part of an Automator workflow on Mac OS X.

Our solution demonstrated a nice way of using Automator as a way to gather information from a user and pass it to a shell script. It’s handy because many users feel intimidated by the Terminal, but are happy to run an Automator action. Here’s what we did…

First, let’s assume that we have two folders on our desktop. One is called “Source” and the other called “Dest”. If we wanted to copy all files whose name contains “Xxx” from Source to Dest then we could fire up the Terminal and do:

find ~/Desktop/Source -type f -name "*Xxx*" -exec cp "{}" ~/Desktop/Dest \;

The “*Xxx*” bit is where we tell the find command that we’re interested in files whose name “Xxx” with any text before or after it. You might want to read up on other options the find command has if you’re not familiar with it – it can be really useful.

However, we don’t want “Xxx”, we want the user to be able to decide what text to search for. So, let’s set up an Automator workflow and add an action to get some that text from the user…

Ask for text

Hopefully that’s straight forward. Next, we add a “Run Shell Script” action:

Run shell script

If you feel like copying and pasting, then the code is:

for f in "$@"
do
find ~/Desktop/Source -type f -name "*$f*" -exec cp "{}" ~/Desktop/Dest \;
done

That’s probably a little less straight forward! So let’s look at it a bit more closely. First, note that “pass input” is set to “as arguments”. This means that the output of the previous action is passed to our shell script as a special variable called $@.

The variable $@ could theoretically be a list, so we use a for loop to go through each item. Odds are that it’ll only ever be one item, but at least this way it’ll still work if one day it is a list. Our for loop goes through the $@ list an item at a time and executes the code between “do” and “done” once for each item in that list (the item is in a variable called $f). The code being executed should be familiar as the find command from earlier, but with $f as the search string instead of “Xxx”

So, if you run the workflow now you should be prompted to enter some text. Your source folder should be scanned for files whose name contains the text you entered, and any matching files copied to the destination folder.

You can run the workflow as it is, or save it as an Application if you prefer. Hopefully users will find this a more user friendly way of passing parameters to a shell script and exceuting it than using the Terminal.

05 Jul

Automated image processing on Mac OS X

cogs

If you’ve ever had to convert a few hundred images from tiff to png, create thumbnails of them you’ll know how time consuming and boring it can be. It can also be surprisingly error prone as your mind drifts off halfway through to think about what you’re up to at the weekend. Computers were supposed to relieve us of this kind of drudgery, right?

Luckily, every Mac has a powerful system lurking under the hood called ‘sips’ (Scriptable Image Processing System). You can use it to do all sorts of interesting things to raster images (eg. jpeg, tiffs, png etc) without ever having to fire up Photoshop. In this article I’m going to show you some simple uses of sips, and how to roll them up into a handy Automator workflow.

A quick into to sips
First, lets fire up the Terminal. It’s just an app that lets us work on the command line and you’ll find it in /Applications/Utilities. Type the following at the prompt:

sips -s format png

…next, drag any image file other than a png file into the Terminal. You should now have something in your Terminal that looks like this:

sips -s format png /path/to/your/file.jpg

That tells sips that we want to change the format of the image we dropped to a png. All we need to do now is to tell it where to put the resulting file. We can do this by adding –out followed by the location of the file we want to be created, so add that into your Terminal like this:

sips -s format png /path/to/your/file.jpg --out /path/to/your/file.png

Note that for the output file path, I just copied and pasted the input file path and changed the extension to “png”. Alternatively, I could have used something like “~/Desktop/file.png” to save the file to my desktop, or I could have used any other path that suits my purpose. Now we have a full sips command, so hit return to execute the command and a file should be created at the location you specified with the –out parameter.

The sips command we just created is a fairly typical one. Note that the word ‘sips’ is followed by some parameters (in our case, “-s format png”), then the path to the source file, then we used –out to tell sips where to put the resulting file. One thing to remember is that having your input file path the same as the output file path is generally a Bad Thing.

There are many other things you can do with sips, including resizing images, changing resolution, applying ICC profiles and so on. The manual (aka man page) is available by typing “man sips” in the Terminal or, if you prefer, click here to view it online.

Using SIPS from Automator
There’s nothing wrong with using SIPS as shown above, but the Terminal isn’t particularly user friendly. It’d be better if we could just select a load of files in the Finder and process them, right? That’s what we’ll look at now. First, launch Automator (you’ll find it in your Applications folder). Create a new workflow, being sure to select “Service” as shown below.

screen 1

Next, set the “Service receives selected” popup menu to “image files” and select “Finder.app”. This means that our workflow will be available when we select images in the Finder.

screen 2
Next, type “shell script” into the search field and drag the “Run Shell Script” action into the area under the popups we just set. Change the “Pass input” drop down to “as arguments”. You workflow should now look like this:

Next, replace the default text “cat” with the following:

for theFilePath in "$@"; do

done

In case you’re not familiar with shell scripting, this is a simple loop. When we execute the Run Shell Script action, it will be passed a list of files called “$@”. What we’ve written is a script to loop through this list. The loop starts at “do” and ends at “done”. Every time the loop repeats, the variable “theFilePath” will be the next file name in the list. Your workflow should now look like this:

screen 4
At the moment, our script doesn’t do anything because there is nothing between “do” and “done”. We need to add some sips magic! In between “do” and “done” add the following:

sips -s format png "$theFilePath" --out "${theFilePath%%.*}.png"

Note that I’m using the theFilePath variable as the input. The confusing stuff as the output just knocks the file extension off the theFilePath variable and adds “.png”. Your workflow should now look like this:

screen 5
Next, save your workflow and give it a name. I chose “Convert Images”.

Okay! Now for the fun bit. Find one or more images (not a PNGs!) in your Finder. Click the file whilst holding “ctrl” down and scroll down to Services, then select your workflow.

services

Your selected files will be converted magically into PNG files! NB. If nothing happens then check you selected “Pass input: as arguments” in the Run Shell Script action.

If I want to create multiple images, then it’s fine to add extra sips commands. So if I want to create a thumbnail image then I can change my shell script to:

for theFilePath in "$@"; do
sips -s format png "$theFilePath" --out "${theFilePath%%.*}.png"
sips -s format png -Z 100 "$theFilePath" --out "${theFilePath%%.*}_thumbnail.png"
done

…see the sips man page to see how the -Z option works!

Hope that gives some you ideas and saves you some time!

21 Jun

How to recognise common barcodes

question

Sometimes you may be asked to recreate a barcode. You’re given a picture of a barcode and told, “Make one like that, but with a different number…”. So you fire up your trusty barcode generator but then realise you don’t know what type of barcode it is. Is it an EAN13 or a UPC-A? Or maybe a Code 39?

You could try every barcode type one by one in the hopes that one looks sort of right. However, that’s time consuming and annoying. Luckily, if you know what to look for then it’s easy to identify some of the more common types of barcode just by sight. Here’s a free guide to help you the next time you need to turn barcode detective.

Click here to download your free guide

Click here to download your free guide

And remember, if you need to create a barcode on Mac OS X then Barcode Basics supports all the barcode types in our guide and many, many more!


Link to Mac App Store to purchase Barcode Basics - Mac barcode software

08 Jun

How to learn shell scripting for OS X

Terminal in Mac OS X

I was recently asked what the best way to learn about shell scripting and the Terminal is for OS X.

The commands available in Terminal can be very powerful, and combining them in a shell script can help you achieve some very impressive things and save yourself a lot of time. And you can combine shell scripts with Automator to make some really cool tools.

Of course, a powerful tool in untutored hands also be pretty dangerous. So it’s important to be sure you know what you’re doing.

The first thing to remember is that when you’re working in Terminal, you’re in a bash shell on a UNIX system. Unix has been around a long time so there are plenty of guides available.

Where to start?

Many moons ago when I was first dabbling with shell scripting, I tried a few books and nothing really clicked until I read this book:

Learning Unix for OS X by Dave Taylor

Click book cover image to view book details

It’s a really good beginner’s guide to UNIX, directly focussing on OS X.

Going deeper…

If you get to grips with Learning Unix for OS X and still crave more information then try:

Click book cover image to view details

Click book cover image to view book details

…just bear in mind that Unix in a Nutshell is a general guide to UNIX rather than OS X specific. That said, the vast majority of it will be applicable and it’ll give you a very comprehensive understanding of UNIX. I know several UNIX system admins who have a sneaky copy of it in a bottom drawer as a handy reference.

Other guides

Of course, there are plenty of good online guides, for example this one:

BASH Programming – Introduction HOW-TO

…and there’s a course on Lynda.com which looks good at first sight, although I’ve not used it so I can’t comment.

If you don’t have time to learn to shell script or if you can’t figure out how to do something then don’t forget that we do custom development at very reasonable rates. Get in touch via our support page for details.

15 Feb

Using TimeMachine to back up to a Windows share


Apple’s TimeMachine provides a really easy way of backing up to an external hard drive, or an Apple-made TimeCapsule on your network. If you want to back up to a non-Apple drive on your network – for example a Windows share – it’s not so straight forward.

In short, what we do is create a sparseimage and copy it to the Windows share mounted on our Mac. Next, we mount that sparse image on the Mac and set it as our TimeMachine disk. Let’s look at that in more detail.

Create the sparseimage

  • Open your Terminal (it lives in /Application/Utilities)
  • Paste the line below into your Terminal, then press return. This should create a sparseimage called “TimeMachine” on your desktop.
  • hdiutil create -size 500g -type SPARSEBUNDLE -fs "HFS+J" ~/Desktop/TimeMachine.sparsebundle

    The 500g bit sets the size of the sparseimage (in Gb). It needs to be at least as big as the Mac’s hard drive, but give it as much as you can on your Windows share.

  • Mount the Windows share on your Mac and copy the sparseimage to the Windows share.
  • Delete the sparseimage from your Mac’s Desktop to avoid confusion
  • Double click the sparseimage on the Windows share. You should notice what appears to be a disk called TimeMachine appear on your Mac’s desktop. This is actually the mounted sparseimage on the Windows share.
  • Set up TimeMachine

  • Next, go back to the Terminal, paste the command below and hit return. This tells Time Machine to use the mounted volume:
  • sudo tmutil setdestination /Volumes/TimeMachine

  • Check in your Mac’s System Preferences that Time Machine is now set to use the new share.
  • What could possibly go wrong?

    There are two potential gotcha’s here. The first one is that backups will only happen when the Time Machine share is visible. You *could* just remember to connect to the Windows share and double click the sparseimage to mount it every time you restart your Mac. If you’re forgetful like me then you can do it automatically with an AppleScript

    try
    mount volume "smb://path/to/your/windows/share"
    end try
    do shell script "hdiutil attach -mountpoint /Volumes/nameofwindowsshare/ /Volumes/nameofyourwindowsshare/TimeMachine.sparsebundle"

    …simply paste this into a Script Editor window, and select save as Application. Add the saved application to your login items (System Preferences>Users & groups) so it mounts the sparseimage automatically on login.

    The second gotcha is that if you need to rebuild your whole Mac (as opposed to just restore files from a particular date) for example after a hard disk failure then you can’t use OS X Recovery. You’ll need to install OS X from scratch, mount the Windows share and sparseimage then use Migration Assistant. Not particularly difficult, but worth making a note of to save you a panic!

    09 Feb

    Acrobat Fix: Preflight audit trail could not be embedded

    If you use Adobe Acrobat on Mac OS X to embed preflight audit trails into PDF files then you may have noticed a strange error saying “Preflight audit trail could not be embedded” appear as of yesterday afternoon (8th February 2016). This is an issue with an expired Adobe certificate and will prevent you embedding your audit trail and will give you an error that looks like this:

    Preflight audit trail could not be embedded

    Preflight audit trail could not be embedded

    How to fix it…
    The error is down to an expired certificate, and the certificate is just a file that lives inside the Adobe Acrobat app. The first thing to do is to get a new copy.

    To do this, download a new trial version of Acrobat whilst making sure you don’t delete your old copy. So, download a copy of Acrobat DC from here. When you get a dialogue box like the one below, make sure that you untick the box that says “Remove old versions”.

    Be sure to untick "Remove old versions"

    Be sure to untick “Remove old versions”

    Once you have Acrobat DC installed, the next job is to find a copy of the new certificate. Find the app in your Applications folder, control click it. Select “Show Package Contents”. This will show you the contents of the app as a folder structure. Look in this folder:

    Contents/Plugins/Preflight.acroplugin/Contents/Resources/

    …and find a file called Preflight.p12. This is the new certificate file.

    Now, find your old copy of Adobe Acrobat, do the “Show Package Contents” trick again and find the Preflight.p12 file. Delete it and replace it with the new certificate file. Note that the Preflight.p12 file is in different locations depending on which version of Acrobat you have. You may have to hunt around for it.

    If you’re on Acrobat XI (as we are) then the location is:

    Contents/Built-in/Preflight.acroplugin/Versions/A/Resources/

    Restart your Adobe Acrobat and you should be able to embed audit trails again.

    28 Jan

    Finding image differences on Mac OS X

    Finding the differences between two images can be quite tricky, especially if those differences are quite subtle. It’s easy to miss things with the human eye, and overlaying images in Photoshop and rapidly toggling between them only makes it slightly easier.

    Luckily, ImageMagick gives us a free set of command line tools to do tasks like this for us. If you don’t have a copy, head over to Cactus Labs and pick up their free installer. It’s by far the easiest way of installing it.

    I’m going to show you how to find the differences between two images using ImageMagick’s compare tool in the Terminal. I’m going to compare two versions of a graphic I have called “demo_image_v1.ai”, and “demo_image_v2.ai” and find out which pixels are different.

    Source images to compare

    First, launch your Terminal – you’ll find it in /Applications/Utilities. Next, type ‘compare’ into your Terminal., followed by a space. It should look something like this.

    compare_2

    Next, drag the first image file into the Terminal. It should automatically fill in the path to the image file. I’m using an image called “demo_image_v1.ai”, which lives in a folder on my desktop called “demo_images”. Note how the path looks in the Terminal. Your path will be different, of course!

    compare_2

    Next drag the second file into your Terminal. You should now have something that looks like this:

    compare_3

    So, what we’ve done so far is tell ImageMagick’s compare tool to find the differences between the two files we dragged onto the Terminal. The last thing to do is to tell it where to save the results. Let’s put the results in a file on the Desktop named, “differences.jpg”. So, type a space in your Terminal and then type:

    ~/Desktop/differences.jpg

    Just in case you’re not familiar with file paths in the Terminal, “~/Desktop” is shorthand for the path to the desktop.

    Your Terminal window should now look something like this:

    compare_4
    So, we’ve now told the compare tool about two files we want to compare, and given it a location to save the results to. Hit return in your Terminal to run the command.

    If you’ve given compare two large images, there may be a bit of a pause. However, you should eventually get a file on your desktop named “differences.jpg” with the pixels which are different between the two images marked in red. Cool, eh?

    differences

    This is just a simple example – there is much more to ImageMagick’s compare tool. For more options, head back to your Terminal and type “man compare” for the full manual.

    Have fun!