Is it possible to skip a file rename when it’s not there in Automator

automationautomator

I have written a little Automator script that does the following tasks:

  1. Get Specified Finder Items – to get one file
  2. Rename Finder Items: Name Single Item – to rename that to .old
  3. Get Specified URLs – to pull a plugin down from a GIT repo
  4. Download URLs – to save the resulting URL to a file
  5. Get Specified Finder Items – to find the renamed .old file from step 1
  6. Move Finder Items to Trash – to remove .old file

The purpose is to grab the latest copy of a TernJS plugin and place it in the correct location for Atom Ternjs plugin for Atom.io. It works well when the file in question is already there, but if it's not it blows up on the Rename step. So, it it possible to either tell the Rename step to never fail OR somehow skip the step in the case where no files were found?

Best Answer

I wrote a small JavaScript for Automation program that may be able to accomplish what you want to do. It checks for and deletes the existing file, then proceeds to download the desired file from the GIT repository:

// Get Finder application
finder = Application('Finder')
finder.includeStandardAdditions = true

// Create path to the location of the existing file
path = Path("path/to/destination")

// Check to see if the old file exists
if (finder.files.length > 0) {
    oldFile = finder.files[0]
    oldFile.delete()
}

// Use curl to download the new file
current = Application.currentApplication()
current.includeStandardAdditions = true
theURL = "http://url/to/git/plugin"
current.doShellScript("curl -L " + theURL + " -o " + path.toString())

Alternatively, you could take some portion of this code (such as the conditional that determines whether there is an existing file) and put it into an Automator "Run JavaScript" action in your existing script.