How to open new application in current activate desktop with AppleScript? - applescript

I want to open a new google chrome window when i press a shortcut key.
I have a LCD connected with MacBook, that means 2 screens.
MacBook have Desktop 1 Desktop 2, LCD have Desktop 3 Desktop 4.
tell application "/Applications/Google Chrome.app"
make new window
open location "https://www.google.com/"
activate
end tell
save this as Quick Action with Automator, and set the shortcut key in keyboard setting.
it works well on the LCD desktops, but can't open chrome on MacBook desktops.
I tried make desktop 1 focused and press shortcut key, It can only open new chrome in current desktop(3 or 4) which on the LCD, never open on the MacBook.

This AppleScript code works for me using the latest version of macOS Mojave.
tell application "Google Chrome"
activate
set newWindow to (make new window)
repeat while loading of active tab of newWindow
delay 0.1
end repeat
set URL of active tab of newWindow to "https://www.google.com/"
end tell
With both of your displays connected, and assuming you have the proper set up in System Preferences (as in the image below), you should just be able to right click Google Chrome icon in your dock and in the options menu... You can choose which display and desktop you want Google Chrome to be displayed.

Related

Applescript open app in certain desktop without switching focus

I am trying to make a simple AppleScript to open Spotify on my computer. I have assigned Spotify to always open on Desktop 2. However, every time it opens, it switches my focus from whatever desktop I am currently using to Desktop 2 where Spotify is being opened. Is there any way for me to prevent this, that is, for the app to open without switching my focus to a different desktop?
Current code:
tell application "Spotify"
activate
delay 3
playpause
end tell
I have also tried to open Spotify using commands like open -g -a 'Spotify', but this just opens the app in the background and still switches desktops.
Thanks for the help.

AppleScript for AirPlay Button in the new TV app

Can anybody figure out how to click the AirPlay Display pop up button in the new TV app (found in MacOS Catalina) using AppleScript? I've tried a zillion different ways, and still get the result "missing value."
As far as I know this code should work:
tell application "System Events"
tell application process "TV"
tell window 1
click ((button whose description is "airplay") of pop up button 1)
end tell
end tell
end tell
Here is a picture of the Accessibility Inspector for that pop up button:
UPDATE: Apparently the new TV app in Catalina is not fully scriptable. AppleScript support for AirPlay Displays does not exist (neither directly nor through GUI scripting). Hopefully, Apple will add support in the future...

Break Active Finder Tab Into New Window with Applescript

I am trying to write an applescript to break the active Finder tab into a new window. I am able to do this with Chrome using:
on moveActiveTabToNewWindow()
tell application "Google Chrome"
set theURL to URL of active tab of window 1
close active tab of window 1
make new window
set URL of active tab of window 1 to theURL
end tell
end moveActiveTabToNewWindow
However, as far as I can tell Finder tabs are not accessible via Applescript. Is this possible? I'm on OS X Mavericks 10.9.2.
Manipulating tabs is not accessible directly but you could do it manually. In other words we can close that tab and then open a new window and replicate what you saw in that tab, just like you did in your Google Chrome example.
Here's some code. This code is basic. Just like I got the "target" property from the Finder window, you could get other properties too and replicate them in the new window to really do a more complete job of replicating the tab. You'll probably want to duplicate the bounds and view options at least.
Good luck.
-- get the target of the front tab
tell application "Finder"
activate
tell window 1 to set t to target
end tell
-- close that tab
tell application "System Events"
keystroke "w" using command down
end tell
-- make a new window and set its target
tell application "Finder"
set w to make new Finder window at front
tell w to set target to t
end tell

Can I assign the "New Window" hotkey in Mac OSX to a particular theme?

In the Mac OSX Terminal app, I want to press command-N and get a new window with the Homebrew theme. I have edited my preferences so that on startup of the Terminal app I get a Hombrew-themed window, but I'd like to be able to create new windows (and tabs if that's not too much to ask for) with that same theme.
I've tried assigning a new hotkey in System Preferences -> Keyboard -> Keyboard Shortcuts -> Application Shortcuts. I created a new hotkey for Terminal, and specified "Homebrew" as the command. This did not work. have I specified the name of the menu command incorrectly, or am I just going about this wrong?
I also dug around a bit in the Terminal.app folder (using the shell), looking for a config file where I could set this, but no luck.
Any ideas?
Thanks!
James
You can change the default profile by clicking the default button under the list of profiles in the second tab.
If others were looking how to create a window with a specific profile, you can change the current settings property or use UI scripting:
tell application "Terminal"
do script ""
set current settings of window 1 to settings set "Homebrew"
end tell
tell application "System Events" to tell process "Terminal"
click menu item "Homebrew" of menu 1 of menu item "New Window" of menu "Shell" of menu bar item 3 of menu bar 1
end tell

Google Chrome doesn't open URL with AppleScript

What I have set up is a AppleScript that asks you what website you want to open with a couple options, to make this simple, I only list one option, Google. This is the current code:
if the_button = "Google" then
site = "http://www.google.com"
tell application "Google Chrome"
launch
tell window 1
make new tab with properties {URL:site}
repeat while loading of active tab
delay 0.1
end repeat
end tell
activate
end tell
It opens a new tab in Google Chrome, but doesn't put anything in the URL bar or reload/load or anything. I've tried this: Script Safari 5.1 to open a tab But, nothing's working. I'm using Mac OS X Lion 10.7.4 and Google Chrome version 21.0.1180.79. Please help!
It works for me, but not when Chrome doesn't have open windows or if all windows are minimized. You could add a reopen command to open a new default window or unminimize a window in those cases.
set site to "http://www.google.com"
tell application "Google Chrome"
reopen
activate
tell window 1
make new tab with properties {URL:site}
end tell
end tell
Another option might be to use something like do shell script "open " & quoted form of "http://google.com" & " -a Google\ Chrome". It's often closer to the default behavior of browsers.
Can't you just do this?
tell application "Google Chrome"
activate
set theSite to "http://www.google.com/"
open location theSite
end tell
The “open location” command is part of Standard Additions and is available to all Mac apps. It’s the default way to open a website in any Mac app. You might notice that Safari’s AppleScript dictionary doesn’t even include a way to open a website — it simply uses the standard “open location” command which you can find in the Standard Additions dictionary.

Resources