Wednesday, January 19, 2022

#1 2022-01-14 12:07:01 pm

slashdot
Member
Registered: 2003-11-13
Posts: 317

macOS - shell script and AppleScript script not working together

Hi All,

I have:
- shell script
-- using terminal runs fine manually using "sh ~/Desktop/tm.sh

- AppleScript
-- creates the file but doesn't output the date into the file

Question:
- what am I doing wrong and why is it not working in the AppleScript?

shell script - tm.sh
-- takes the latest backup date for Time Machine and outputs it to a file (tmDate.txt) on the desktop.

#!/bin/sh
enabled=`/usr/bin/defaults read /Library/Preferences/com.apple.TimeMachine AutoBackup`
if [ "$enabled" == "1" ];then
lastBackupTimestamp=`date -j -f "%a %b %d %T %Z %Y" "$(/usr/libexec/PlistBuddy -c "Print Destinations:0:SnapshotDates" /Library/Preferences/com.apple.TimeMachine.plist | tail -n 2 | head -n 1 | awk '{$1=$1};1')" "+%Y-%m-%d %H:%M:%S"`
echo "$lastBackupTimestamp"
else
echo "<result>Disabled</result>"
fi
echo "$lastBackupTimestamp" > ~/Desktop/tmDate.txt



Applescript

Applescript:

do shell script "sh ~/Desktop/tm.sh"

Last edited by slashdot (2022-01-15 01:46:24 am)

Offline

 

#2 2022-01-15 01:13:19 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: macOS - shell script and AppleScript script not working together

slashdot wrote:

Question:
- what am I doing wrong and why is it not working in the AppleScript?


In my opinion, the tilde is recognized by the Terminal (as the path to the home folder). But for the do shell script command, it means nothing. That is, you should most likely specify the path to your home folder instead of the tilde:

Applescript:


set thePath to quoted form of (POSIX path of (path to home folder) & "Desktop/tm.sh")

do shell script "sh " & thePath


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-01-15 01:15:09 am

slashdot
Member
Registered: 2003-11-13
Posts: 317

Re: macOS - shell script and AppleScript script not working together

thanks for the response but I still get an empty file with no date.

using:

Applescript:

set thePath to quoted form of (POSIX path of (path to home folder) & "Desktop/tm.sh")

do shell script "sh " & thePath

Offline

 

#4 2022-01-15 01:30:35 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: macOS - shell script and AppleScript script not working together

What happens when you indicate the full path to sh ?

do shell script "/bin/sh " & allPath


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2022-01-15 01:34:05 am

slashdot
Member
Registered: 2003-11-13
Posts: 317

Re: macOS - shell script and AppleScript script not working together

Applescript:

set thePath to quoted form of (POSIX path of (path to home folder) & "Desktop/tm.sh")

do shell script "/bin/sh " & thePath

creates the file but it's empty.

Last edited by slashdot (2022-01-15 01:34:30 am)

Offline

 

#6 2022-01-15 01:48:54 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: macOS - shell script and AppleScript script not working together

I ran your sh script in Terminal and it created an empty tmDate.txt file on my desktop. Perhaps this is because I do not make any backup at all.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#7 2022-01-15 02:02:47 am

slashdot
Member
Registered: 2003-11-13
Posts: 317

Re: macOS - shell script and AppleScript script not working together

Ok here's the solution: roll

Solution:
- Privacy setting wasn't set for Full Disk Access

for Script Editor or Automator.

Once that is set then it works as desired.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)