licknau Posted August 20, 2016 Share Posted August 20, 2016 Hi, Does anyone know if there's an Alfred workflow to move the mouse to the active window. This would be very useful for my 3 monitor setup, as I can easily use the application switcher to get to desired window but it takes (what feels like ages) to get the mouse there. My google searches are polluted by "focus-follows-mouse" content... which is the opposite of what I'm looking for! I also see a bunch of options for Windows users through AutoHotKey scripts, so I know that this is a commonly desired functionality. So, has anyone made this for Alfred? If not, is it possible and what tools and resources are available for it? Thanks in advance! Link to comment
deanishe Posted August 21, 2016 Share Posted August 21, 2016 (edited) This script moves the mouse cursor to the centre of the frontmost window. Put it in a Run Script action with Language = /usr/bin/osascript (JS). I have no idea if it works with multiple monitors. ObjC.import('stdlib') ObjC.import('CoreGraphics'); // Move mouse cursor to specified position function moveMouse(x, y) { var pos = $.CGPointMake(x, y); var event = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, pos, $.kCGMouseButtonLeft); $.CGEventPost($.kCGHIDEventTap, event); } // Run script function run() { var app = Application.currentApplication(); app.includeStandardAdditions = true; var win = app.windows[0], bounds = win.properties().bounds; // Calculate centre of window var x = Math.trunc(bounds.x + (bounds.width / 2)), y = Math.trunc(bounds.y + (bounds.height / 2)); console.log('centre = ' + x + 'x' + y); moveMouse(x, y); } Edited August 21, 2016 by deanishe licknau 1 Link to comment
christianmagill Posted November 9, 2021 Share Posted November 9, 2021 This script doesn't seem to work anymore unfortunately. [00:47:45.661] ERROR: Mouse to Active Window[Run Script] 0:5: syntax error: A unknown token can’t go after this identifier. (-2740) Link to comment
vitor Posted November 9, 2021 Share Posted November 9, 2021 13 hours ago, christianmagill said: This script doesn't seem to work anymore unfortunately. You’re running it in /usr/bin/osascript (AppleScript) instead of /usr/bin/osascript (JavaScript). Link to comment
christianmagill Posted November 11, 2021 Share Posted November 11, 2021 You're right! Thanks... Unfortunately, that brings me to this error. [21:54:43.754] ERROR: Mouse to Current Window[Run Script] /Users/christianmagill/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/FB91CFD4-D374-483B-BF36-6AFDE1DAF46D: execution error: Error: TypeError: undefined is not an object (evaluating 'win.properties') (-2700) Link to comment
deanishe Posted November 11, 2021 Share Posted November 11, 2021 Which application isn't it working with? Link to comment
christianmagill Posted November 11, 2021 Share Posted November 11, 2021 Seems to be all of them. I've tried a variety, 3rd party, Apple, etc. This is how I have things setup: Link to comment
deanishe Posted November 11, 2021 Share Posted November 11, 2021 56 minutes ago, christianmagill said: Seems to be all of them. In that case, it might be a permissions problem. Or Apple has changed something since Catalina. Link to comment
vitor Posted November 11, 2021 Share Posted November 11, 2021 4 hours ago, deanishe said: Or Apple has changed something since Catalina. Works for me on Big Sur. Link to comment
christianmagill Posted November 11, 2021 Share Posted November 11, 2021 (edited) Possibly, I get the same error on both my Monterey machines. M1 and Intel. I've checked all the permissions, and even added screen recording to the mix as I know sometimes that's necessary to get window titles, etc. Edited November 11, 2021 by christianmagill Link to comment
deanishe Posted November 11, 2021 Share Posted November 11, 2021 Try this instead: ObjC.import('stdlib') ObjC.import('CoreGraphics'); // Move mouse cursor to specified position function moveMouse(x, y) { let pos = $.CGPointMake(x, y); let event = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, pos, $.kCGMouseButtonLeft); $.CGEventPost($.kCGHIDEventTap, event); } // Run script function run() { let appName = Application('System Events').processes.whose({frontmost: {'=': true}})[0].name(); console.log(`frontmost application = ${appName}`); let app = Application(appName); app.includeStandardAdditions = true; let win = app.windows[0], bounds = win.properties().bounds; // Calculate centre of window let x = Math.trunc(bounds.x + (bounds.width / 2)), y = Math.trunc(bounds.y + (bounds.height / 2)); console.log('centre = ' + x + 'x' + y); moveMouse(x, y); } I think it’s a massive bug in the original. I was using Application.currentApplication(), which will be the application running the script, not the frontmost application. I suspect that it works for Vítor and me because we’re running the script in Script Editor, which has windows, and is both the frontmost app and the app running the script. It won't work in Alfred because it will look for Alfred's window (not the active application's), which is probably not visible. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now