Python Forum
Opening and closing Mac applications and files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opening and closing Mac applications and files
#1
So I've been trying to open and close Mac OS. applications and files with python but have had little to no success can anyone provide me with any clue on how to do this.

For example one would be open Mac OS. calendar
Reply
#2
If you want to launch an app and wait for the user to exit, you could use /usr/bin/open and subprocess.run. If you want to continue the program after launching, could use subprocess.Popen.

>>> import subprocess
>>> subprocess.run(["/usr/bin/open", "-a", "calendar"])  # waits until calendar exit
CompletedProcess(args=['/usr/bin/open', '-a', 'calendar'], returncode=0)
>>> subprocess.Popen(["/usr/bin/open", "-a", "calendar"]) # returns immediately
<subprocess.Popen object at 0x1065e3048> 
I'm not sure if there's a more direct interface that avoids /usr/bin/open.
Reply
#3
is there an argument that I could make to close the calendar app as well not have the user close it
Reply
#4
Maybe, but not through this method. I'm not an apple developer, so I don't know what's common for them, but there appears to be a javascript interface to some applications.
Javascript for automation

I don't know how well that works, and I don't know if there's any similar python interface to that kit. Hopefully someone else around here will be more familiar with it.
Reply
#5
would it be the same way to open a file and would there be a way to close a file
Reply
#6
To close app from terminal in MacOS one can use AppleScript or pkill:

osascript -e 'quit app "Calendar"'

pkill -x Calendar
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Phyton Opening files on windows C: pc EddieG 3 936 Mar-29-2023, 03:19 PM
Last Post: buran
  opening files and output of parsing leodavinci1990 4 2,456 Oct-12-2020, 06:52 AM
Last Post: bowlofred
  Closing Files - CSV Reader/Writer lummers 2 2,564 May-28-2020, 06:36 AM
Last Post: Knight18
  Internationalization of applications ? JohnnyCoffee 1 1,714 Apr-17-2020, 09:39 PM
Last Post: Larz60+
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 1,989 Mar-03-2020, 04:47 AM
Last Post: davidshq
  PYHTON not opening files profficial 11 13,358 Feb-15-2020, 09:06 AM
Last Post: bharathendra
  Automating Windows GUI applications metro17 4 12,176 Feb-10-2020, 09:46 AM
Last Post: metro17
  Handling Thick client applications using Python PraveenSubramaniyan 1 3,177 Jul-22-2019, 01:08 PM
Last Post: DeaD_EyE
  opening a file from a windows desktop folder- error opening file Charan007 1 2,931 Dec-06-2018, 11:50 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020