Tag Archives: AppleScript

XtraFinder error message “XtraFinder Beta has expired!” fix with applescript

I was recently asked to help with the annoying XtraFinder’s message “XtraFinder Beta has expired!” and make it disappear without user input. As what was suggested in the official support page did not work I went to see what is available online, Google is your friend, right!? ūüôā

Well unfortunately I found only one good and interesting blog post on the subject involving Hopper Disassembler, but that did not worked for me, whatever I tried I did not manage to produce an executable file which will actually work. The tutorial is for XtraFinder version 0.25 where mine was 0.25.8 and though the assembler instructions and the function looked the same I think that matters.

As I am not an assembler guru I went back to the good old applescript to make a few clicks on behalf of a human ūüôā Please check the script below and change it¬†if you need to. The script app has to be in your log in items and will/should work fine even after you upgrade to the latest version of XtraFinder, currently 0.25.9 /which by the way fixes the nag messages, until the time for the next update probably, which if not on time will probably start the receiving of the expire message again.. or may be not… :-)/.

So here we go:

 

delay 3

tell application “XtraFinder” to activate

delay 1

tell application “System Events”

¬†if exists (window 1 of process “XtraFinder”) then

¬† tell process “XtraFinder”

¬† ¬†set XfinderMess to the value of static text 1 of window 1 of application process “XtraFinder” of application “System Events”

¬† ¬†if XfinderMess = “System Integrity Protection is enabled.” then

¬† ¬† click button “OK” of window 1 of application process “XtraFinder” of application “System Events”

    delay 10

¬† ¬† tell application “System Events”

¬† ¬† ¬†if exists (window 1 of process “Finder”) then

¬† ¬† ¬† tell process “Finder”

¬† ¬† ¬† ¬†set finderMess1 to the value of static text 1 of window 1 of application process “Finder” of application “System Events”

¬† ¬† ¬† ¬†if finderMess1 = “XtraFinder Beta has expired!” then

¬† ¬† ¬† ¬† click button “OK” of window 1 of application process “Finder” of application “System Events”

        delay 6

¬† ¬† ¬† ¬† tell application “System Events”

¬† ¬† ¬† ¬† ¬†if exists (window 1 of process “Finder”) then

¬† ¬† ¬† ¬† ¬† tell process “Finder”

¬† ¬† ¬† ¬† ¬† ¬†set finderMess2 to the value of static text 1 of window 1 of application process “Finder” of application “System Events”

¬† ¬† ¬† ¬† ¬† ¬†if finderMess2 = “You‚Äôre up-to-date!” then

¬† ¬† ¬† ¬† ¬† ¬† click button “OK” of window 1 of application process “Finder” of application “System Events”

           end if

          end tell

         else 

          quit

         end if 

        end tell

       end if

      end tell

     else

      quit

     end if

    end tell

   end if

  end tell

 else

  quit

 end if

end tell

 

Enjoy ūüôā

 


AppleScript execution depending on system event

Some time ago I had to create a script to execute on startup, do its job and reboot the computer if needed, but how do you prevent going in to rebooting loop? Luckily after a bit of wondering around I managed to find a solution Рthe system_profiler command! It gives the opportunity to track the hardware state and if there is any change it can be used to trigger a script execution.

I do not know about your experience with Macs and multiple displays, but according to mine it can get messy, especially if you have more then two displays or /god forbid! ;-)/ Apple plus other display manufacturers.

In my case the problem was not having all the displays on after initial boot, the main Philips display comes on, but both Apple Thunderbolt displays did not. After a reboot though everything comes back to normal. The task was to make a script checking the state of the screens and automatically reboot if there is a problem.

So this is the script I have made to solve the problem, you can tweak it the way you want and need /may be if you use it or post it somewhere mentioning me would be nice :-)/:

delay 15
set display to (do shell script “system_profiler SPDisplaysDataType | grep Thunderbolt; echo $?”)
if display = “1” then
¬†¬†¬†¬†do shell script “open /Path/To/Your/Scripts/Reboot.app”
else
¬†¬†¬†¬†display notification “No Need to Reboot!”
    delay 5
    quit
end if

Reboot.app is just a script to reboot the computer, you can make your own, use my previous posts or use any other means to achieve the goal.

And finally – if you insist on having your application windows exactly on the screen you want and at the location you want them to be, there is one very good application called Stay, it is not free, but does the job nicely.

Enjoy!


ApleScript examples for Mac OS X El Capitan

Time is tight as usual, so let‚Äôs not waste it and get on with the already delayed El Capitan post. ūüôā

As some of you already know Apple introduced a new security feature to their latest OS X called System Integrity Protection (SIP). What it basically does is to limit access to sensitive parts of the OS, prevent code injection, etc. This is all fine probably for most users, but limits the possibilities of tweaking the system as well as flexibility of manipulating certain aspects of the OS’s behaviour.

So this brings some inconvenience when you try to automate certain aspects of your daily routine. For example the bless command will not work, you can no longer ‚Äúempty trash‚ÄĚ while a file is still engaged in a process, if disable SIP you can no longer do repair permissions on your system files as this feature has been removed from Disk Utility.

Simply said ‚Äď if you want the full control over the OS you will have to disable SIP. I am not going in to details on how to do it, there are many articles on internet about that. Here are just few links if you are interested:

Apple’s official article Рhttps://developer.apple.com/library/mac/documentation/Security/Conceptual/System_Integrity_Protection_Guide/ConfiguringSystemIntegrityProtection/ConfiguringSystemIntegrityProtection.html

 

XtraFinder’s article on how to partially disable SIP Рhttps://www.trankynam.com/xtrafinder/sip.html

or this one for some more customisation – https://www.reddit.com/r/osx/comments/3hv3kk/update_on_rootless_the_configuration_mechanism/

 

Below you will find two AppleScripts, one for automating the process of repairing file permissions and the other one is for empting your trash.

First the script which does the permission repair. Before using it you will need to download and install the utility from here https://www.firewolf.science/2015/07/repairpermissions-v2-0-cli/

Make sure you follow the installation guidance, read the notes section ‚Äď it is kind of important.

Alternatively if you do not want to use this utility you may prefer to do it differently by using repair_packages command following this tutorial: http://lifehacker.com/verify-and-repair-permissions-from-the-command-line-in-1741718667

 

And the script itself, change the necessary bits with your data or change it the way it suits you:

 

display dialog¬†“Repair Disk Permissions”¬†buttons¬†{“VolumeName 1”, “Volume Name 2”, “QUIT”}¬†default button¬†“QUIT”

if¬†button returned¬†of¬†result¬†= “QUIT”¬†then

     quit

else

     set diskVol to the button returned of the result as text

¬† ¬† ¬†tell¬†application¬†“Terminal”

          activate

¬† ¬† ¬† ¬† ¬† set¬†RP¬†to¬†do script¬†“sudo /usr/local/bin/RepairPermissions \”/Volumes/” & diskVol¬†& “\” “

          delay 1

¬† ¬† ¬† ¬† ¬† tell¬†application¬†“System Events”

¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†keystroke¬†“YourPassword” &¬†return

          end tell

          set mainID to id of front window

¬† ¬† ¬† ¬† ¬† close¬†(every¬†window¬†whose¬†id¬†‚Ȇ¬†mainID)

          repeat until busy of RP is false

               delay 1

          end repeat

     end tell

end if

delay 30

tell¬†application¬†“Terminal”¬†to¬†quit

 

The second script for empting the trash looks a bit bloated, but as AppleScript has no understanding of ‚Äúgo to‚ÄĚ statement /at least I could not find any referral to it, though for some reason this statement is considered a bad practise/ I had no choice but to leave it as it is. Any suggestions are welcome.

As per my previous post regarding AppleScript you will have to adjust some details in the script:

 

 

do shell script¬†“sudo nvram SystemAudioVolume=%80”¬†password¬†“YourPass”¬†with¬†administrator privileges

do shell script¬†“defaults write com.apple.loginwindow TALLogoutSavesState -bool false”¬†password¬†“YourPass”with¬†administrator privileges

set¬†trashcontents¬†to¬†quoted form¬†of¬†(do shell script¬†“ls ~/.Trash”)

if¬†trashcontents¬†= “”¬†then

¬† ¬† ¬†tell¬†application¬†“Finder”¬†to¬†activate

¬† ¬† ¬†tell¬†application¬†“System Events”

¬† ¬† ¬† ¬† ¬† tell¬†process¬†“Finder”

¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†click¬†menu item¬†13¬†of¬†menu¬†1¬†of¬†menu bar item¬†“Apple”¬†of¬†menu bar¬†1

          end tell

     end tell

    delay 5

¬† ¬† ¬†tell¬†application¬†“System Events”

¬† ¬† ¬† ¬† ¬† tell¬†process¬†“loginwindow”

               activate

               click button 2 of window 1

          end tell

     end tell

else

¬† ¬† ¬†do shell script¬†“sudo rm -rf ~/.Trash/*”¬†password¬†“YourPass”¬†with¬†administrator privileges

     delay 10

¬† ¬† ¬†tell¬†application¬†“Finder”¬†to¬†activate

¬† ¬† ¬†tell¬†application¬†“System Events”

¬† ¬† ¬† ¬† ¬† tell¬†process¬†“Finder”

¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†click¬†menu item¬†13¬†of¬†menu¬†1¬†of¬†menu bar item¬†“Apple”¬†of¬†menu bar¬†1

          end tell

     end tell

     delay 5

¬† ¬† ¬†tell¬†application¬†“System Events”

¬† ¬† ¬† ¬† ¬† tell¬†process¬†“loginwindow”

               activate

               click button 2 of window 1

          end tell

     end tell

end if

 

And that‚Äôs it for now, hope this helps not just me, but someone else too ūüôā

 

 

 


Useful AppleScript examples

Here are some useful /I think/ AppleScript examples which can be used on their own or in other scripts. The presumption here is you already have an idea of how to use the Script Editor. Keep in mind some scripts might not behave as it should depending on your OS X and will need to be slightly adjusted. I will do separate post just for EL CAPITAN when I got some time. ūüôā

Ways to open and close applications with apple script:

If you want or need to use the Terminal to open an application:

 

tell application “Terminal”

¬†¬†¬†¬†¬† do script “open /Applications/Safari.app; exit”

end tell

delay 2

tell application “Terminal” to quit

 

But if you prefer the short way of doing it you can just type

 

do shell script “open /Applications/Safari.app; exit”

 

or

 

tell application “Safari” to activate

 

And to close application few options are as follows

 

tell application “Safari” to quit

 

or

 

do shell script “killall Safari”

 

Closing Finder application will simply reload it, which is sometimes necessary

 

do shell script “killall Finder” password “YourPassword” with administrator privileges

 

Managing optical drives:

When the case is to manage optical drives using applescript my personal opinion is this is best done with the use of drutil command. To find more about the command you can man it and in terminal window you can:

 

$ drutil info

 

As an example to close the tray of optical drive with the name ‚ÄúDVD-RW GH41N‚ÄĚ you can:

 

tell application “Terminal”

¬†¬†¬†¬†¬† do script “drutil tray close -drive ‘DVD-RW GH41N’; exit”

end tell

delay 3

tell application “Terminal” to quit

 

or if it is external one you can just

 

tell application “Terminal”

¬†¬†¬†¬†¬† do script “drutil tray close -drive external; exit”

end tell

delay 3

tell application “Terminal” to quit

 

To open the tray

 

tell application “Terminal”

¬†¬†¬†¬†¬† do script “drutil eject -drive ‘DVD-RW GH41N’; exit”

end tell

delay 3

tell application “Terminal” to quit

 

Automating shutdown and restart:

If the case is to shutdown or restart the computer with just a click of a button you can use the scripts below. They will also do some additional tasks like switching off the option of restarting the active application on boot, silence the startup chime, empty the trash can, which you may of course skip.

Reboot using terminal command:

 

do shell script “defaults write com.apple.loginwindow TALLogoutSavesState -bool false” password “YourPassword ” with administrator privileges

delay 2

do shell script “nvram SystemAudioVolume=%80” password “YourPassword ” with administrator privileges

delay 2

do shell script “shutdown -r now” password “YourPassword” with administrator privileges

 

Reboot using Mac OS X menu, mind though menu item 13 in the example might not be correct for you, so adjust it if necessary:

 

do shell script “defaults write com.apple.loginwindow TALLogoutSavesState -bool false” password “YourPassword” with administrator privileges

delay 2

do shell script “sudo nvram SystemAudioVolume=%80” password “YourPassword” with administrator privileges

delay 2

tell application “Finder” to activate

tell application “System Events”

¬†¬†¬†¬†¬† tell process “Finder”

¬†¬†¬†¬†¬†¬† click menu item 13 of menu 1 of menu bar item “Apple” of menu bar 1

      end tell

end tell

delay 3

tell application “System Events”

¬†¬†¬†¬†¬† tell process “loginwindow”

       activate

       click button 2 of window 1

      end tell

end tell

 

To shutdown the computer, following the same order and logic as above use the examples below. Again mind menu item 15, the number might not be the same for you.

 

tell application “Finder”

      empty trash with security

end tell

do shell script “shutdown -h now” password “YourPassword” with administrator privileges

 

or

 

tell application “Finder”

      empty trash with security

end tell

delay 10

tell application “Finder” to activate

tell application “System Events”

¬†¬†¬†¬†¬† tell process “Finder”

¬†¬†¬†¬†¬†¬† click menu item 15 of menu 1 of menu bar item “Apple” of menu bar 1

      end tell

end tell

delay 3

tell application “System Events”

¬†¬†¬†¬†¬† tell process “loginwindow”

       activate

       click checkbox 1 of window 1

       click button 2 of window 1

      end tell

end tell

 

Changing startup disk:

Something else which like me you can find useful is to quickly reboot to another Mac OS X installation. The first script is using script with buttons, which is faster, but Apple decided you do not need more then 3 buttons in a script dialog. So if you are having more than 3 bootable drives /some people do, trust me :-)/ you have to use a script with a list. Reboot.app in the examples is another script to reboot the computer, but you can do it differently.

The example script with the buttons:

 

display dialog “Select a startup disk” buttons {“NameOf HD1”, “NameOf HD2”, “QUIT”} default button “QUIT”

if button returned of result = “QUIT” then

      quit

else

      set bootVol to the button returned of the result as text

¬†¬†¬†¬†¬† do shell script “bless -mount \”/Volumes/” & bootVol & “\” -setBoot” password “YourPassword” with administrator privileges

¬†¬†¬†¬†¬† tell application “Reboot.app” to activate

end if

 

And for a list it might be something like this:

 

set Disks to {“1”, “2”, “3”, “4”, “5”}

set selectedDisk to {choose from list Disks with title “Startup Disk” with prompt “Choose Disk” default items “2” without multiple selections allowed and empty selection allowed}

do shell script “bless -mount \”/Volumes/” & selectedDisk & “\” -setBoot” password “YourPassword” with administrator privileges

tell application “Reboot.app” to activate

 

This is just an example script showing how to control an application with System Events, in this case Skype. Here the preferred web cam is picked from the list of available devices.

 

tell application “Skype” to activate

delay 5

tell application “System Events”

¬†¬†¬†¬†¬† tell process “Skype”

¬†¬†¬†¬†¬†¬† click menu item 3 of menu 1 of menu bar item “Skype” of menu bar 1

      end tell

end tell

delay 5

tell application “System Events”

¬†¬†¬†¬†¬† tell process “Skype”

       set winName to the name of window 1

¬†¬†¬†¬†¬†¬† if winName = “General” then

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† click button “Audio/Video” of toolbar 1 of window 1

                   click pop up button 2 of window 1

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† click menu item “FaceTime HD Camera (Display)” of menu 1 of pop up button 2 of window “Audio/Video”

       else

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† tell application “System Events” to keystroke “w” using command down

       end if

      end tell

end tell

 

That is quite enough for now, future scripts will be in separate posts, hope you found this information useful. Enjoy ūüôā