Sharing some things I've learned

Mac OSX specific forum
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Syntax Highlighting

Post by Piero »

AZJIO wrote: Fri May 05, 2023 7:49 pm I made regular expression highlighting
https://prismjs.com
 
Powerful Javascript! :shock:
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Dedicated to Fred ;)

Post by Piero »

I dedicate this quick applescript hack to Fred, because I cannot hear any PureBasic sound on my mac ;) ;) ;) and I want to port this to PB (for windows ppl too!) without having to warn anyone to avoid using double quotes on file/folder names ;) ;) ;)
 

Code: Select all

(* -- by Piero

PLEASE BE SURE TO READ BELOW FOR "INSTALLATION" INSTRUCTIONS.

This creates a "Loudmaxed" copy of dropped audio or video files.
Audio will be enhanced!
You can also choose files via dialog by running it (double click).
Multichannel (surround) sound will be made stereo.
If a video is multi-language/multi-audio, you can choose the right track to process.
If a video is subtitled, you can optionally keep one of the subtitles.
Copies are saved on the same folder of the original, and will be overwritten if you process the original again.

by Piero

Export this script as an application, after you opened it with Script Editor...
You can easily change the icon of the exported app:
https://support.apple.com/lv-lv/guide/mac-help/mchlp2313/mac

You need to download and "install" 3 free softwares (put into the finder window that will open when you run the "incomplete" version of this app).

You can also normally install LoudMax.vst into Library/Audio/Plug-Ins/VST/.
Works with Homebrew ffmpeg (/opt/homebrew/bin/ffmpeg) and mrswatson64 can be put into /opt/homebrew/bin/ too.

ffmpeg
https://www.osxexperts.net/  ( https://evermeet.cx/ffmpeg/   https://ffmpeg.org/ )

mrswatson64
https://github.com/teragonaudio/MrsWatson/blob/master/bin/Mac%20OS%20X/mrswatson64

LoudMax.vst
https://pluginguru.net/loudmax-for-mac/  ( https://loudmax.blogspot.com/ )


"standing on the shoulders of giants"...

*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property outvol : "0.94" -- do not set volume to more than 0.94

property addtoname : "_LM"

on ex(ff) -- avoids applescript "exists" bug
	try
		do shell script "test -e " & ff
		return true
	on error
		return false
	end try
end ex

on notinst(sss, ooo)
	tell application "Finder"
		activate
		open (((path to me) as text) & "Contents:Resources:") as alias
		display dialog sss & " not installed!\n\nPlease drag into the Finder window that just opened!" & "\n\n\n" & ooo buttons ("OK") default button "OK"
	end tell
end notinst

on updpr(stri, nu)
	set progress description to stri
	set progress completed steps to nu
	delay 0.34
end updpr

on open thefiles
	set tempfolder to path to temporary items
	try
		set ptm to quoted form of POSIX path of (path to me)
		
		if not ex("/Library/Audio/Plug-Ins/VST/LoudMax.vst") then
			if not ex(ptm & "Contents/Resources/LoudMax.vst") then
				otinst("Loudmax", "...or into /Library/Audio/Plug-Ins/VST/\n\nhttps://loudmax.blogspot.com/")
				return
			end if
		end if
		
		set ffm to "/opt/homebrew/bin/ffmpeg"
		if not ex(ffm) then set ffm to ptm & "Contents/Resources/ffmpeg"
		if not ex(ffm) then
			otinst("Ffmpeg", "...or install with homebrew (/opt/homebrew/bin/)\n\nhttps://www.osxexperts.net/\n\nhttps://ffmpeg.org/\n\nhttps://evermeet.cx/ffmpeg/")
			return
		end if
		
		set mrs to "/opt/homebrew/bin/mrswatson64"
		if not ex(mrs) then set mrs to ptm & "Contents/Resources/mrswatson64"
		if not ex(mrs) then
			notinst("mrswatson64", "https://github.com/teragonaudio/MrsWatson/blob/master/bin/Mac%20OS%20X/mrswatson64")
			return
		end if
		
		set lmthre to word 1 of text returned of (display dialog "Loudmax Threshold\n\nExample: 0.02 will not enhance very low volumes\n\nSuggested values: from 0.0 to 0.5" buttons {"Cancel", "OK"} default button "OK" with icon caution default answer "0.0")
		
		set filecount to count thefiles
		set ii to 1
		
		repeat with i in thefiles
			set quotedinputfile to quoted form of POSIX path of i
			set progress total steps to 4 --filecount
			set progress additional description to "Processing File " & ii & " of " & filecount & ":\n\n" & quotedinputfile
			updpr("Analyzing File Tracks...\n\nHold ⌘. to Stop.\n", 0)
			set quotedtempfolder to quoted form of (POSIX path of tempfolder)
			set tmpextractedpcm to quotedtempfolder & "lmaudioin.pcm"
			set tmploudmaxedpcm to quotedtempfolder & "lmaudioout.pcm"
			
			set filname to ((characters 1 thru -((offset of "." in (the reverse of (characters of (POSIX path of i))) as text) + 1) of (POSIX path of i)) as text) & addtoname
			set quotfilname to quoted form of filname
			set filext to ((characters -1 thru -((offset of "." in (the reverse of (characters of (POSIX path of i))) as text)) of (POSIX path of i)) as text)
			set atr to "0"
			set str to "0"
			set sot to "1"
			set trstr to "AUDIO TRACKS:" & return
			set atrax to ""
			try
				set atrax to (do shell script ffm & " -i " & quotedinputfile & "  2>&1 | grep -E 'Stream.*Audio' | grep -En '.'") as string
			end try
			set trstr to trstr & atrax
			
			set stra to ""
			try
				set stra to (do shell script ffm & " -i " & quotedinputfile & " 2>&1 | grep -E 'Stream.*Subtitle' | grep -En '.'") as string
				set trstr to trstr & return & return & "SUBTITLE TRACKS:"
				set trstr to trstr & return & stra
			on error
				set sot to "0"
			end try
			if ((count paragraphs of atrax) > 1) or ((count paragraphs of stra) > 0) then
				if filecount > 1 then beep
				activate
				set da to text returned of (display dialog quotedinputfile & return & return & trstr & return & return & "1  1  means 1st audio track, 1st subtitle track\n1  0  means 1st audio track, no subtitles" buttons {"Cancel", "OK"} default button "OK" default answer "1   " & sot & "  ")
				set atr to (word 1 of da as integer) - 1
				set str to (word 2 of da as integer) - 1
			end if
			
			updpr("Extracting Audio...\n\nHold ⌘. to Stop.\n", 1)
			do shell script ffm & " -y -i " & quotedinputfile & " -q:a 0 -map 0:a:" & atr & "\\? -f s16le -ar 44.1k -ac 2 " & tmpextractedpcm
			
			updpr("Loudmaxing...\n\nHold ⌘. to Stop.\n", 2)
			do shell script mrs & " -p loudmax --parameter 0," & lmthre & " --parameter 1," & outvol & " --parameter 3,1.0 -i " & tmpextractedpcm & " -o " & tmploudmaxedpcm & "; rm " & tmpextractedpcm --  2> /dev/null
			
			updpr("Saving Audio...\n\nHold ⌘. to Stop.\n", 3)
			
			do shell script ffm & " -y -i " & quotedinputfile & " -f s16le -ar 44.1k -ac 2 -i " & tmploudmaxedpcm & " -c:v copy -map 0:v:0\\? -map 1:a:0 -map 0:s:" & str & "\\? " & quotfilname & filext & "; rm " & tmploudmaxedpcm
			
			set ii to ii + 1
			updpr("Loudmaxing Done!\n\nHold ⌘. to Stop.\n", 4)
			
		end repeat
		
		try
			say "Loudmaxing. Done!" using "zarvox" volume 1.0
		on error
			beep 3
		end try
	on error x number n
		set gu to 600
		if n is -128 then
			set gu to 3
			try
				say "Loudmaxing Stopped!" using "zarvox" volume 1.0 without waiting until completion
			on error
				beep
			end try
		else
			try
				say "Loudmaxing Error!" using "zarvox" volume 1.0 without waiting until completion
			on error
				beep
			end try
		end if
		--activate
		display dialog x buttons {"OK"} default button "OK" with icon stop giving up after gu
		display dialog "Do you want to open the temporary items folder?" buttons {"OK", "NO!"} default button "NO!" cancel button "NO!"
		tell application "Finder"
			activate
			open tempfolder
			--set w1 to (a reference to window 1)  -- to close it later
		end tell
	end try
end open

on run
	set ptm to quoted form of POSIX path of (path to me)
	if (not ex(ptm & "Contents/Resources/mrswatson64") and not ex("/opt/homebrew/bin/mrswatson64")) or (not ex("/opt/homebrew/bin/ffmpeg") and not ex(ptm & "Contents/Resources/ffmpeg")) or (not ex("/Library/Audio/Plug-Ins/VST/LoudMax.vst") and not ex(ptm & "Contents/Resources/LoudMax.vst")) then
		tell application "Finder"
			activate
			open (((path to me) as text) & "Contents:Resources:") as alias
			display dialog "Please download the needed files\nffmpeg\nLoudMax.vst\nmrswatson64\nand drag them into the Finder window that just opened!\n\nSee Readme file for links" buttons ("OK") default button "OK"
		end tell
		return
	end if
	
	--	Handle the case where the script is launched without any dropped files
	open (choose file with prompt "Select audio or video files to Loudmax:" with multiple selections allowed)
end run
Last edited by Piero on Sun Jul 30, 2023 6:04 am, edited 1 time in total.
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

I always forget…

Post by Piero »

…you can now send notifications, if your applescript does a lot of stuff in background:
 
display notification "I'm done!" with title "Did my stuff," subtitle "You Silly Italian!" sound name "sosumi.aiff"
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Sharing some things I've learned

Post by Piero »

xGestures
version 1.9.1
© 2004-2022 by Brian Kendall
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

-- Toggle wifi (Applescript)

set device to do shell script "networksetup -listallhardwareports | awk '$3==\"Wi-Fi\" {getline;print}' | awk '{print $2}'"

set power to do shell script "networksetup -getairportpower " & device & " | awk '{print $4}'"

if power is equal to "on" then
    set power to "off"
    
beep
    
-- say "wi-fi " & power
else
    set power to "on"
end if

do shell script ("networksetup -setairportpower " & device & " " & power)

display alert "wi-fi " & power giving up after 1
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

Command - Shift - .
(⌘⇧.)
to "reveal invisible stuff" in Finder
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: Sharing some things I've learned

Post by deseven »

Piero wrote: Fri Jun 09, 2023 1:07 pm -- Toggle wifi (Applescript)
If you're using shell scripting so extensively you might as well get rid of AppleScript completely, could be faster.

Code: Select all

#!/bin/bash

device=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline;print}' | awk '{print $2}')
power=$(networksetup -getairportpower $device | awk '{print $4}')

if [ "$power" == "On" ]; then
    power="Off"
    echo -ne '\007' # beep
    say "wi-fi $power"
else
    power="On"
fi

networksetup -setairportpower $device $power
osascript -e "display alert \"wi-fi $power\" giving up after 1" # could be replaced with terminal-notifier as well
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

deseven wrote: Tue Jun 27, 2023 10:44 am If you're using shell scripting so extensively you might as well get rid of AppleScript completely, could be faster.
You are right; but often I use AS just to quickly add dialogs, use drag and drop etc.
I wish I could do it with PB too, but... ( see viewtopic.php?t=81520)

-- erase finder selection

tell application "Finder"
    set s to selection
    
if s is {} then return
    
set p to {}
    
repeat with i in s
        copy quoted form of POSIX path of (i as alias) to the end of p
    end repeat
    
activate
    
set theResult to choose from list p ¬
        with title "Items to DELETE" with prompt "WARNING!!!" default items p ¬
        
OK button name "DELETE" cancel button name "STOP" multiple selections allowed 1 ¬
        
empty selection allowed 0
    if theResult is not false then
        repeat with i in theResult
            do shell script "rm -rf " & i --with administrator privileges
        end repeat
        
update items --with necessity
    end if
end tell
Last edited by Piero on Sun Jul 30, 2023 6:58 am, edited 2 times in total.
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

I updated the "dedicated to Fred" post above (loudmax audio) to work with some malformed videos
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

To remove that silly orange dot on top-right of screen when you used old stuff like https://www.menubus.audio/versions (enhance your mac audio with audio plugins, for free!) type:
sudo killall coreaudiod
in terminal
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

Script Debugger (even when trial ends)
Latest (update apps)
Skim (pdf viewer)
Cyberduck (ftp client etc.; "free" if downloaded from web site)
Velja ("open links")
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

https://obdev.at (launchbar, little snitch)
They are very generous with their "free use" policy... there are free alternatives but...
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

Using the copy and set Commands
As its name implies, when you use the copy command to create a variable, it always creates a separate copy (though note that a copy of an object specifier still specifies the same object). However, when you use the set command to create a variable, the new variable always refers to the original object or value. You have essentially created another name for the same object.
When more than one variable refers to a changeable (or mutable) object, a change to the object is observable through any of the variables. The types of AppleScript objects that are mutable are date, list, record, and script objects.
For objects that cannot be modified (immutable objects), variables created with the set command may seem like copies—there’s no way to change the object the variables point to, so they seem independent.
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

https://www.macosxautomation.com/applescript/sbrt/

Note: if your subroutine doesn't seem to work, add "my", like:
set a to my subroutine(stuff)
User avatar
Piero
Enthusiast
Enthusiast
Posts: 319
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Sharing some things I've learned

Post by Piero »

https://yourscriptdoctor.com/applescript-date/

...because I don't see date manipulation examples on sbrt (essential applescript subroutines)
I imagine shell/C/assembler coders screaming while seeing things like this, but it can be useful…
Locked