开发者

AppleScript to load a volume mount, start an application, and restart if the mac comes out of suspend.

开发者 https://www.devze.com 2023-03-23 13:22 出处:网络
I am having some issues with my Mac connecting to an SMB share on my network, and then loading an application immediately after that.

I am having some issues with my Mac connecting to an SMB share on my network, and then loading an application immediately after that.

For the most part, everything works when the mac turns on just by setting the volume to mount on login as usual, and also run the application (XBMC) on login also.

Occasionally though, for no reason that I have been able to pin down after an extensive amount of troubleshooting, the auto mounting of a volume some times fails as it believes the network location is unavailable. As a result the Mac can't create the volume mount unless I restart the Mac, then it works again.

Now I want an AppleScript that will attempt to create the volume mount three(3) times, and then load XBMC. If the vo开发者_如何学运维lume can't be mounted after 3 attempts, force the Mac to restart. This would then cause the script to run from scratch again after a restart.

How would I achieve this in AppleScript?

Second problem:

I have my Mac set to go to suspend after 1 hour of inactivity. The only problem is that if the Mac has been suspended for some time, upon waking up XBMC can't load remotely stored content some of the time.

So, is it therefore possible to have a script run when the Mac resumes from being suspended, that makes the Mac perform a restart?

Thanks to anyone who has read the entirety of my post, I realise it is a bit of a rant.

Regards.


Try this for your first question. As far as your "suspend" question I don't know the answer offhand. However I would look into launchd. You can probably write a launchd plist file which is run when the mac resumes and that launchd plist would just run the applescript using the command line tool osascript.

set remoteDiskName to "Disk Name"
set remoteIPAddress to "192.168.1.xxx"
set user_name to "userName"
set pass_word to "password"

repeat 3 times
    set success to mountSMB(remoteDiskName, remoteIPAddress, user_name, pass_word)
    if success then exit repeat
    delay 1
end repeat

if success then
    -- load XBMC
else
    tell application "Finder" to restart
end if

on mountSMB(remoteDiskName, remoteIPAddress, user_name, pass_word)
    if remoteDiskName is in (do shell script "/bin/ls /Volumes") then
        return true
    else
        set theAddress to quoted form of ("smb://" & user_name & ":" & pass_word & "@" & remoteIPAddress & "/" & remoteDiskName)
        set mountpoint to quoted form of ("/Volumes/" & remoteDiskName)
        try
            do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_smbfs " & theAddress & space & mountpoint
            return true
        on error
            try
                do shell script "/bin/rm -r " & mountpoint
            end try
            return false
        end try
    end if
end mountSMB
0

精彩评论

暂无评论...
验证码 换一张
取 消