[SOLVED] Opening file whenever USB device is attached (fails)

I am trying to create a daily backup of a file named "My Clippings.txt" from my Amazon Kindle reader. It has all the highlights from all books on my Kindle. This is how I try to do it:

  • A macro is triggered by a USB device (being my Kindle reader) when it is attached.
  • And then, I do a bunch of things, that work, whenever I try the macro sequence manually by hitting the "Try" button. But whenever the sequence is triggered by the attached USB device I get this error:

"Open File failed with non-existent path"

The path (/Volumes/Kindle/documents/My Clippings.txt) does exist. I even delay my macro execution two seconds before I start the other commands. But without success.

In trying to debug my macro, I eliminited all other steps and kept only one:

"Open /Volumes/Kindle/documents/My Clippings.txt" whenever my Kindle device is attached to my Mac. But again, without success.

What am I missing?

Update

I ended up replacing the copy/open part with a "Execute Shell Script" where I did the copy/open part in Python like so:

#!/usr/bin/env python

import os
import shutil

var_ts = os.environ['KMVAR_MeinTimestamp']
fpath = "/Volumes/Kindle/documents/My Clippings.txt"
dest_path = "/Users/ugur/Documents/ABLAGE Ugur/Kindle Notes Annotationen/{}_kindle_annotations.txt".format(var_ts)

# copy file
shutil.copyfile(fpath, dest_path)

# Now open dest path
os.system("open '{}'".format(dest_path))

And that also works only when I run the macro manually, but it does not when it's triggered by USB being attached. In that case I get an error message again (looking at the logfile, I see this):

IOError: [Errno 2] No such file or directory: '/Volumes/Kindle/documents/My Clippings.txt'

2020-04-28 17:04:54 Traceback (most recent call last):
  File "text-script", line 11, in <module>
    shutil.copyfile(fpath, dest_path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 96, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '/Volumes/Kindle/documents/My Clippings.txt'
 Macro “Backup Kindle - My Clippings.txt” cancelled (while executing Execute Shell Script).

Is the macro triggered too fast? Before the USB device can be detected as "attachted"? Or what could cause the problem?

Update 2 (solved)

I added a 5 sec delay as an initial step. That seems to be enough time for the next steps to recognize the volume.

1 Like