PSmac.psm1

$script:ModuleRoot = $PSScriptRoot

function Empty-MacOSTrash {
    # Empty the Trash on macOS per https://superuser.com/questions/1877663/how-can-i-empty-the-trash-from-the-macos-terminal
    osascript -e 'try' -e 'tell application "Finder" to empty' -e 'end try'
}

function Restart-MacOSApp { 
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateScript(
            { $_ -in (Get-Process).Name },
            ErrorMessage = 'Please specify the name of a subdirectory in the current directory.'
        )]
        [ArgumentCompleter(
            {
                param($cmd, $param, $wordToComplete)
                # This is the duplicated part of the code in the [ValidateScipt] attribute.
                [array] $validValues = (Get-Process).Name
                $validValues -like "$wordToComplete*"
            }
        )]
        [String]$AppName
    )
        
    Get-Process $AppName | Stop-Process 
    Start-Sleep -Seconds 1 
    open -a "$AppName"
}


function Stop-MacOSApp { 
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateScript(
            { $_ -in (Get-Process).Name },
            ErrorMessage = 'Please specify the name of a subdirectory in the current directory.'
        )]
        [ArgumentCompleter(
            {
                param($cmd, $param, $wordToComplete)
                # This is the duplicated part of the code in the [ValidateScipt] attribute.
                [array] $validValues = (Get-Process).Name
                $validValues -like "$wordToComplete*"
            }
        )]
        [String]$AppName
    )
        
    Get-Process $AppName | Stop-Process 
}

# Commands run on module import go here
# E.g. Argument Completers could be placed here

if (!$IsMacOS) {
    Write-Warning "This module is only supported on macOS. Importing it on other platforms may lead to unexpected behavior or errors."
}
else {
    Write-Verbose "Running on macOS, proceeding with module initialization."

    # Init Brew if it exists.
    if (Get-Item -Path /opt/homebrew/bin/brew -ErrorAction SilentlyContinue) {
        /opt/homebrew/bin/brew shellenv | Invoke-Expression
    }
}




# Module-wide variables go here
# For example if you want to cache some data, have some module-wide config settings, etc. ... those could go here
# Example:
# $script:config = @{ }

Export-ModuleMember -Function 'Empty-MacOSTrash','Restart-MacOSApp','Stop-MacOSApp'