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:
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.
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?
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.
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.
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
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)
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)"
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.
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!)