Change Mac wallpaper through AppleScript

I’m trying to specify a jpg file to be the wallpaper for a particular desktop, but it doesn’t seem to work… It instead changes the background into another image in a completely different folder… anyone know how to fix it?

tell application "System Events"
tell current desktop
    set picture to "/Downloads/yosemite-5796x3531-5k-4k-wallpaper-el-capitan-forest-osx-apple-3962.jpg"
end tell

end tell

AppleScript: how to eject/unmount NAS?

I’m trying to eject/unmount a mounted NAS.
It has been mounted with:

set serverName to "MyNAS"
tell application "Finder" to open location ("afp://" & theUser & ":" & thePass & "@" & serverName)

I tried:

tell application "Finder" to do shell script "diskutil umount \"" & serverName & "\"" --***
tell application "Finder" to eject serverName

But it returns a -10010 error: appleEvent can’t handle objets of this class.

Thank you for your help.

Renameing a bunch of quicktimes in applescript

I have a bunch of files in a folder named something like this:

123456_this_is_a_fun_test_v01.mov

685954_this_more_is_a_fun_test_v01_clean.mov

They all have a 6 digit number in the beginning and a version number somewhere. What i need to do is remove the version number and move the first 6 digits til the end, before the extension name, like this:

this_is_a_fun_test_123456.mov

this_more_is_a_fun_test_clean_685954.mov

Been trying some stuff out in automator and some simple Applescripting, but without any luck. My scripting skills are not good, I’m only at “Hobby” level. Anyone got some advice?

tell application "Finder"
--grab the selected files and put them into a variable
set F to selection


 end tell
-- This will be the character used to rejoin pieces
-- of the filename after breaking them apart
set the text item delimiters to "_"

 repeat with g in F -- Loop through the file list

-- Get the filename
tell application "System Events" to get name of g

-- Eliminate the version number component of the filename
set r to do shell script ¬
    "echo " & quoted form of result & ¬
    " | egrep -o '[^_\\.]+'" & ¬
    " | egrep -iv 'v\\d+'"

-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"

-- Rename the file to the new name
tell application "System Events" to set name of g to result

end repeat

Accessing items in a list in AppleScript?

I’m trying to access items in a list via AppleScript. Try as I might, I can’t seem to access them. I’ve pasted my code below

tell application “JSON Helper”
set result to fetch JSON from “https://newsapi.org/v2/top-headlines?sources=bbc-news&pageSize=1&apiKey=X

set news to title of articles of result

end tell

set result_string to news & “”

note, I’ve removed my api key.

The api format is :

{“status”:”ok”,”totalResults”:10,”articles”:[{“source”:{“id”:”bbc-news”,”name”:”BBC News”},”author”:”BBC News”,”title”:”Trump urges Israeli ‘care’ on settlements”,”description”:”The US president also casts doubt on whether the Palestinians or Israel are ready to talk peace.”,”url”:”http://www.bbc.co.uk/news/world-middle-east-43025705“,”urlToImage”:”https://ichef.bbci.co.uk/news/1024/branded_news/165CD/production/_99979519_044058382.jpg“,”publishedAt”:”2018-02-11T16:47:27Z”}]}

I’m trying to access title but I keep getting “can’t get title”.

Any input would be much appreciated. Thanks!

How to move folders to a new location with apple script

I don’t know how to proceed to set up an apple script which moves folders with their content to a new loacation, where these folders migth already exist. If so Finder should overwrite these folders. But in the same process I have specific files, too, which should be moved to a new location. I don’t get it how to make it work.

These are the files and folders I want tonmove and overwrite:
OLDFOLDER > source, which should be overwriten with destination > NEWFOLDER

folders

  • /Volumes/Netshare/Maxon/OLDFOLDER/library/scripts
  • /Volumes/Netshare/Maxon/OLDFOLDER/Off_Plugins

folders

  • /Users/name/Library/Preferences/MAXON/OLDFOLDER/library/browser
  • /Users/name/Library/Preferences/MAXON/OLDFOLDER/prefs

documents (without ending)

  • /Users/name/Library/Preferences/MAXON/OLDFOLDER/prefs/xxx_net_80
  • /Users/name/Library/Preferences/MAXON/OLDFOLDER/prefs/xxx_net_80_version4

documents (with ending)

  • /Users/name/Library/Preferences/MAXON/OLDFOLDER/prefs/c4d_M_GLOBAL_POPUP.res

I would appreciate any help which gives a direection how to realize this. I tried some research before, but did not get ahead…

Best

Add inverted commas as text in AppleScript

I’m trying to make an AppleScript where you change your Mac’s local hostname. I’m trying to do it through a terminal command, but if the user wants spaces in the hostname, It doesn’t work, as for the command with spaces to work, you need inverted commas, and as far as I’m aware of, you can’t do add ” as text in AppleScript. Here’s what i’m trying to do:

set computername to display dialog "Type the desired name" default answer "" with icon note buttons {"Continue"} default button "Continue" as text
display dialog (text returned of computername) & " is this correct? " & "."

display dialog "If it askes you, please type your password in. (It may ask more than once)"

do shell script "scutil --set LocalHostName " & """ & (text returned of computername) & """

display dialog "Changes will be applied after a restart. Click continue to save changes and restart."

do shell script "shutdown -r now"

I need to enter ” before the name is entered, as well as after.

 do shell script "scutil --set LocalHostName " & """ & (text returned of computername) & """

but I just get errors

Thanks in advance

How to get the title of the current Aquamacs window using applescript

I’m trying to programmatically retrieve the major mode of aquamacs. I had a few ideas: get the menubar items, get the window title and parse it using regexes.

I’ve tried both, and ran into a problem, both the menubar and window arrays are empty, which makes it impossible to do this:

on test()
try
    tell application "System Events"
        if application "Aquamacs" exists then
            -- display notification "It's aquamacs"
            tell application "Aquamacs" to activate
            if menu bar item "File" of menu bar 1 exists then
                display notification "File exists"
                --Fails when the file menu bar item clearly is there
            else
                display notification "File doesn't exist"
            end if
        else
            display notification "It isn't aquamacs"
        end if
    end tell

end try
end test

test()

or this:

on getAppTitle(appN)

tell application appN
    activate
end tell

tell application "System Events"
    # Get the frontmost app's *process* object.
    set frontAppProcess to first application process whose frontmost is true
end tell

# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
    if (count of windows) > 0 then --never runs because count is always zero
    set window_name to name of every window

    end if
end tell
end getAppTitle

getAppTitle("Aquamacs")

and then looking at the file extension.

I don’t understand why there’s such inconsistency between the system and AppleScript: it clearly has windows which definitely have titles, yet somehow are out of reach of scripts.

Is there a library equivalent of Apple Script (osascript)?

Is there a way to do the tasks that Apple Script allows without doing system("osascript ...")? In other words, what does osascript do under the hood?

It links with libDiagnosticMessagesClient, libEdit, AppKit, Carbon, Foundation, ApplicationServices, CoreFoundation and CoreServices so the answer must lie in one of those.

How can I click OK with Apple Script?

I’m trying to select OK with the popup for scaling the screen with Apple script.

Does anyone know what I can add to this script to allow me to click OK?

Apple Script:

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
    click radio button "Scaled" of radio group 1 of tab group 1
    click radio button 1 of radio group 1 of group 1 of tab group 1

    (* Trying to click the ok button here *)
    delay 1
    click ((pop up buttons of sheet 1 of window 1) whose description is "OK")

end tell

quit application "System Preferences"

enter image description here

Or also rather does anyone know a trusted application that can graphically print out the labels of the UI when I visit them on my screen so I will know what to use when I say: “tell UIName“?

Thanks.

Automator script to automatic toggle menu setting in Simulator.app

I need to automate the process of enrolment of Face ID and Touch ID for my UITests. For this purpose, I’m working on an automator script.

My current automator script, which at the moment can automatic click “Enrolled” in the menu:

on run {input, parameters}

if application "Simulator" is running then
    tell application "System Events"
        set theName to name of the first process whose frontmost is true
    end tell
    tell application "Simulator" to activate
    tell application "System Events"
        tell process "Simulator"
            tell menu bar 1
                tell menu bar item "Hardware"
                    tell menu "Hardware"
                        tell menu item "Face ID"
                            tell menu "Face ID"
                                click (menu item "Face ID" where its name starts with "Matching")
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
    tell application theName to activate
end if
return input
end run 

The problem is as following. I need to determine, if the device already is enrolled. There is a checkmark, which shows the current state. I have tried to check if the checkmarks is there or not. But I have not been able to make it work yet.

enter image description here

So my question. How can I do, so the script only will press the ‘Enrolled’ menu item, if the checkmark isn’t there?