Jump to content

How do I make my code compatible with Mac?

38034580

The code below is a graphical program to manage functions. It is fully compatible with Windows, but the power controls won't work on Mac. How do I make them work on mac?

from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import *
from pygame import mixer
from os import system
from tkinter.messagebox import *
from pynput.keyboard import Key,Controller
from subprocess import run, PIPE
import screen_brightness_control as sbc
keyboard = Controller()
def audioEngine():
    if mixer.get_init():
        mixer.quit()
        audioButton.config(text='Start Audio Engine')
        fileButton.config(state=DISABLED)
        playButton.config(state=DISABLED)
        pauseButton.config(state=DISABLED)
        stopButton.config(state=DISABLED)
    else:
        mixer.init()
        root.channel=mixer.find_channel()
        audioButton.config(text='Stop Audio Engine')
        fileButton.config(state=NORMAL)
        playButton.config(state=NORMAL)
        pauseButton.config(state=NORMAL)
        stopButton.config(state=NORMAL)
def volume(v):
    if v==2:
        keyboard.press(Key.media_volume_up)
        keyboard.release(Key.media_volume_up)
    elif v==1:
        keyboard.press(Key.media_volume_down)
        keyboard.release(Key.media_volume_down)
    else:
        keyboard.press(Key.media_volume_mute)
        keyboard.release(Key.media_volume_mute)
def shutdown(arg):
    system('shutdown -'+arg+' -t 0')
def play():
    try:
        if root.paused:
            root.channel.unpause()
        else:
            root.channel.play(mixer.Sound(root.file))
        root.paused=False
    except:
        showerror('Error', "The file can't be played or there is no file loaded.")
def pause():
    root.channel.pause()
    root.paused=True
def stop():
    root.channel.stop()
    root.paused=False
def file():
    f=askopenfilename(filetypes=[('WAV Files', '*.wav'),('MP3 Files', '*.mp3'),('M4A Files', '*.m4a'),('WMA Files','*.wma'),('MIDI Files', '*.mid')])
    if f=='':
        root.file=None
    else:
        root.file=f
def brightness(b):
    if b==1 and sbc.get_brightness()<100:
        sbc.set_brightness(sbc.get_brightness()+1)
    elif sbc.get_brightness()>0:
        sbc.set_brightness(sbc.get_brightness()-1)
    bright.config(text=str(sbc.get_brightness()))
def runprocess():
    try:
        run(cmd.get())
    except OSError as e:
        showerror("Can't Run Process", e)
def detect():
    bright.config(text=str(sbc.get_brightness()))
    root.after(100, detect)
root=Tk()
root.file=''
root.iconbitmap('control.ico')
root.paused=False
menu=Menu(root, tearoff=0)
optionmenu=Menu(menu, tearoff=0)
menu.add_cascade(label="Options", menu=optionmenu)
root.resizable(False, False)
root.title('Windows Control Center v1.0')
optionmenu.add_command(label='Connect Smartphone/Smartwatch...', command=lambda: showinfo('Coming Soon', 'Connecting a smartphone/smartwatch will be supported probably in the next update.'))
frame1=LabelFrame(root, text='Power Controls')
frame1.grid()
Button(frame1, text='Suspend', command=lambda: system('rundll32.exe powrprof.dll,SetSuspendState 0,1,0')).grid(column=0, row=0)
Button(frame1, text='Power Off', command=lambda: shutdown('s')).grid(column=1, row=0)
Button(frame1, text='Restart', command=lambda: shutdown('r')).grid(column=2, row=0)
root.config(menu=menu)
frame4=LabelFrame(root, text='User Controls')
frame4.grid()
Button(frame4, text='Lock', command=lambda: system('Rundll32.exe user32.dll,LockWorkStation')).grid(column=0, row=0)
Button(frame4, text='Sign out', command=lambda: shutdown('l')).grid(column=1, row=0)
frame2=LabelFrame(root, text='Audio Controls')
frame2.grid()
volup=Button(frame2, text='+', command=lambda: volume(2))
volup.grid(column=2, row=0)
voldn=Button(frame2, text='-', command=lambda: volume(1))
voldn.grid(column=1, row=0)
voldn=Button(frame2, text='X', command=lambda: volume(0))
voldn.grid(column=0, row=0)
frame3=LabelFrame(root, text='Media Player')
frame3.grid()
audioButton=Button(frame3, text='Start Audio Engine', command=audioEngine)
audioButton.grid(column=0, row=0)
fileButton=Button(frame3, text='File', command=file, state=DISABLED)
fileButton.grid(column=4, row=0)
playButton=Button(frame3, text='|>', command=play, state=DISABLED)
playButton.grid(column=1, row=0)
pauseButton=Button(frame3, text='||', command=pause, state=DISABLED)
pauseButton.grid(column=2, row=0)
stopButton=Button(frame3, text='X', command=stop, state=DISABLED)
stopButton.grid(column=3, row=0)
frame5=LabelFrame(root, text='Display Controls')
frame5.grid()
Button(frame5, text='-', command=lambda: brightness(0)).grid(column=0, row=0)
bright=Label(frame5, text=sbc.get_brightness())
bright.grid(column=1, row=0)
Button(frame5, text='+', command=lambda: brightness(1)).grid(column=2, row=0)
frame6=LabelFrame(root, text='Command Prompt')
frame6.grid()
cmd=Entry(frame6)
cmd.grid(column=0, row=0)
Button(frame6, text='Run', command=runprocess).grid(column=0, row=1)
detect()
root.mainloop()

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ideally, use a library that takes care of that for you. Right now you're using a system call

system('shutdown -'+arg+' -t 0')

that's always going to be operating system specific. If you want to go that route, you have to detect what OS you're on, then use whatever system calls are needed for that particular OS.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Eigenvektor said:

Ideally, use a library that takes care of that for you. Right now you're using a system call

system('shutdown -'+arg+' -t 0')

that's always going to be operating system specific. If you want to go that route, you have to detect what OS you're on, then use whatever system calls are needed for that particular OS.

Just to extend off this, this is easily done with the 'system' library in python. The platform() method will return exactly this 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Slottr said:

Just to extend off this, this is easily done with the 'system' library in python. The platform() method will return exactly this 

What's the mac version of the function?

Link to comment
Share on other sites

Link to post
Share on other sites

A quick search for "mac shutdown command line" points to shutdown or halt.

 

https://osxdaily.com/2017/08/13/shutdown-mac-command-line/

 

But on mac/linux you'll need to be root to do it so that might cause issues. 

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

58 minutes ago, 38034580 said:

What's the mac version of the function?

? its a method from the system library. It's built into python, not OS based

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Kilrah said:

A quick search for "mac shutdown command line" points to shutdown or halt.

 

https://osxdaily.com/2017/08/13/shutdown-mac-command-line/

 

But on mac/linux you'll need to be root to do it so that might cause issues. 

should i do this?

def shutdown():
	system('shutdown -'+arg+' now')
Button(root, label='Power Off', command=lambda: shutdown('h'))
Button(root, label='Restart', command=lambda: shutdown('r'))

 

Link to comment
Share on other sites

Link to post
Share on other sites

@Kilrah is right that the shutdown command on macOS is different from Windows. There's the standard unix-y command "shutdown" and there's also "pmset". But for both of those you do need to be root.

 

However, if you want to do it without root that's possible as long as you're the only logged in user.

You can use applescript

osascript -e 'tell app "System Events" to shut down'
Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, maplepants said:

@Kilrah is right that the shutdown command on macOS is different from Windows. There's the standard unix-y command "shutdown" and there's also "pmset". But for both of those you do need to be root.

 

However, if you want to do it without root that's possible as long as you're the only logged in user.

You can use applescript

osascript -e 'tell app "System Events" to shut down'

how about restart or suspend?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, 38034580 said:

how about restart or suspend?

You bet. Here's the excert from the Script Editor dictionary

 

Power Suite

Terms and Events for controlling System power

log outv : Log out the current user

 

restartv : Restart the computer

 

shut downv : Shut Down the computer

 

sleepv : Put the computer to sleep

 

Don't forget about that if "state saving preference" is omitted or false, state is always saved.

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×