AppleScript: Hide/Get process name from app

£可爱£侵袭症+ 提交于 2019-12-24 01:44:48

问题


I want to hide the frontmost app. I know you can hide a process using the syntax:

tell application "System Events"
    set visible of process "..." to false
end tell

and i know how to get the frontmost app:

(path to frontmost application as string)

But how do I bridge the two commands together?

This will not work:

tell application "System Events"
    set visible of process (path to frontmost application as string) to false
end tell

回答1:


Try this.

tell application "System Events"
    set frontProcess to first process whose frontmost is true
    set visible of frontProcess to false
end tell

You have to be careful because in some ways when you run the script, the script is frontmost so you may just end up hiding the script instead of the application you were targeting. I check the name of the frontmost process, and if that matches the name of the script or "applescript runner" then you need to hide that, then run the command again and you'll actually get the application you were targeting. It's tricky.




回答2:


Since most applications will hide if you press Cmd-H, you could also probably go that route.

tell app "System Events" to keystroke "h" using command down


来源:https://stackoverflow.com/questions/5913738/applescript-hide-get-process-name-from-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!