Append date to file name not functioning

I am working with a script that works on my MacBook Pro running El Capitan, however one line of code is failing on my iMac running El Capitan and I haven’t been able to determine why. The line of code is used to copy the safari bookmarks file and append the current date and the:

“cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_date +%Y%m%d-%H%M%S

Like I said, it works on my MacBook Pro but not my iMac both running the same OS X.

Appreciate any insight anyone can provide.

As I’m not a sooth sayer, I’m unable to guess if you get an error message when the instruction fails.

Here,

do shell script "cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_`date +%Y%m%d-%H%M%S`"

works flawlessly on my iMac.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) dimanche 31 janvier 2016 16:35:09

I got the same error as before:

Script Error

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file

cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

I’m only new to applescript but the only difference I see is that the tope line points to the target_file and the bottom line points to target_directory.

My iMac is over 5 years old, but I can’t see that being any reason.

Hi.

Your script works fine for me too. I can duplicate the error message if I make some deliberate mistake in the parameters, such as leaving out the space between the two paths or inserting a space into one of them. Is the script on your iMac exactly right? Did you type it out separately or is it a physical copy of the one which works on your MacBook Pro?

Hi,

Yes it is an exact copy that I copies from one to the other , no typing. It’s a puzzler isn’t it?

Just to be sure, in AppleScript don’t use


"cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_`date +%Y%m%d-%H%M%S`"

but use

do shell script "cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_`date +%Y%m%d-%H%M%S`"

The first syntax is the one used in Terminal.
My iMac is a 2011 one too.

Would be useful to double check that your quotes, simple or double, are the straight ones. If one of them is a curly one the script issue the described message.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) lundi 1 février 2016 12:09:21

That is correct, within the script the line is:

do shell script “cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_date +%Y%m%d-%H%M%S

The only thing that is causing the problem is date +%Y%m%d-%H%M%S. If I remove or replace it with hard code it works. For some reason this is what I think is causing the issue. Like I said, there is no issue with it on my MacBook Pro that is running the same OS X El Capitan.

May you try to run :

do shell script "date +%Y%m%d-%H%M%S"

I’m wondering if the Unix file date isn’t available or is corrupted.
On my machine it’s available as /bin/date.
Curious detail, when I look at its info (cmd + i) I get 28368 bytes (12 Ko on disk)
System Events return : physical size : 12288 bytes

I was wrong, in the complete instruction, date +%Y%m%d-%H%M%S is not enclosed between straight single quotes which force the script to append the string date +%Y%m%d-%H%M%S to the file name.
It is encapsulated between the character `(grave accent) which is a diacritical one (at least here in french).

If it appears that the culprit is the character supposed to encapsulate the date component you may split the instruction into two ones :

do shell script "date +%Y%m%d-%H%M%S"
do shell script "cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_" & result

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mardi 2 février 2016 11:24:59

Why not the “long” version

set theDate to do shell script "/bin/date +%Y%m%d-%H%M%S"
set bookmarksFile to POSIX path of (path to library folder from user domain) & "Safari/Bookmarks.plist"
do shell script "/bin/cp " & quoted form of bookmarksFile & space & quoted form of (bookmarksFile & "_" & theDate)

Hello Stephan

Is this syntax supposed to be OK ?

do shell script "/bin/cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_`date +%Y%m%d-%H%M%S`"

in which I assume that specifying only once the folder storing the UNIX file would be sufficient.

Asking that I assume that when the asker calls the instruction the system is not defaulting to the folder /bin to search for the UNIX file.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mardi 2 février 2016 11:42:43

It’s not about the full UNIX path to the binaries, that doesn’t matter.
I try always to avoid tilde expanding in do shell script lines.

What if you try the recommended format for command substitution? It’s highly recommendable to use the $(command) format rather than backquotes. Also scripts (automation) shouldn’t use tilde paths either because its returning variable $HOME and doesn’t consider the actual home folder, a tilde can return any value.

set lib to POSIX path of (path to library folder from user domain)
do shell script "cp " & quoted form of (lib & "Safari/Bookmarks.plist") & space & quoted form of lib & "Safari/Bookmarks.plist_$(date +%Y%m%d-%H%M%S)"

If you still insist on using tilde in the shell then set the $HOME variable at beginning of your script:

set homeFolder to text 1 thru -2 of POSIX path of (path to home folder)
do shell script "$HOME=" & quoted form of homeFolder & "
cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_$(date +%Y%m%d-%H%M%S)"

I was assuming that if the problem was related to tilde expansion the error message would have been : No such file or directory" number 1

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mardi 2 février 2016 12:17:14

You’re right. Normally I don’t bother either of the two things but since the code of the TS doesn’t work it’s better to be safe.

As I am curious, I wish to know if the replacement of backquote by the $ scheme is sufficient to solve the problem on the OP’s iMac

do shell script "cp ~/Library/Safari/Bookmarks.plist ~/Library/Safari/Bookmarks.plist_$(date +%Y%m%d-%H%M%S)"

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mardi 2 février 2016 12:31:41

It seems overkill, but with FileManagerLib it doesn’t matter if there’s a ~, a POSIX path, or an HFS path:

use FMLib : script "FileManagerLib"
use scripting additions

set thePath to "~/Library/Safari/Bookmarks.plist"
set theDate to do shell script "/bin/date +%Y%m%d-%H%M%S"
FMLib's copyItem:thePath toMake:(thePath & "_" & theDate) withReplacing:true

Yvan your 05:35:05 worked. I went back to the publisher and asked them to review the thread and the recommended changes. Thanks to all for a solution and a great exchange of information.

Hi rsstorey.

Presumably you mean the second script in post #8 above, which was last edited by Yvan at 11:35:05 his time or at 10:35:05 mine. The post and edit times seen in this forum depend on the GMT offsets set in members’ profiles. (But in fact the forum software parochially calculates the times to display as fixed offsets from the local time at the server, not as offsets from GMT. This means that for two or three weeks twice a year, the times seen by members in Europe are an hour out, the US and Canada having switched to Daylight Saving Time and back on different dates from us!)