0

I have a function in my .bash_profile that should allow me to add startup items. The only issue is this echo's a string instead of running the Apple Script.

This is my function

function addtologin(){
    if [ $3 = "hidden" ]; then
        osascript -e 'tell application "System Events" to make new login item at end with properties {path:"${1}/${2}.app", name:"${2}", hidden:true}'
    else
        osascript -e 'tell application "System Events" to make new login item at end with properties {path:"$1/$2.app", name:"$2", hidden:false}'
    fi
}

When I run addtologin /Users/iProgram/Applications Spotify hidden it outputs login item ${2}.

Why is this? Why doesn't it adding Spotify to the Login items?

If it helps, here is my full .bash_profile incase it is causing an issue (don't think it is)

.bash_profile

source .colors
export PS1=${BOLD}${CYAN}\\W\ \\A$\ ${DEFAULTCOLOR}
export SUDO_PS1=\\W\ \\A#\
export PATH=$PATH:/Users/iProgram/bin
export EDITOR=/usr/bin/vim

function ipaddress(){
    ifconfig en0 | awk '/inet / {print $2}'
}

function macaddress(){
    ifconfig en0 | awk '/ether/ {print toupper($2)}'
}

function wifi-off(){
    networksetup -setairportpower en0 off
}

function wifi-on(){
    networksetup -setairportpower en0 on
}

function wifi-connect(){
    networksetup -setairportnetwork en0 $1 $2
}

function nano(){
    vim $@
}

function home(){
    cd ~
}

function addtologin(){
    if [ $3 = "hidden" ]; then
        osascript -e 'tell application "System Events" to make new login item at end with properties {path:"${1}/${2}.app", name:"${2}", hidden:true}'
    else
        osascript -e 'tell application "System Events" to make new login item at end with properties {path:"${1}/${2}.app", name:"${2}", hidden:false}'
    fi
}

.colors

export BLACK='\033[0;30m'
export RED='\033[0;31m'
export GREEN='\033[0;32m'
export BROWN='\033[0;33m'
export BLUE='\033[0;34m'
export PURPLE='\033[0;35m'
export CYAN='\033[0;36m'
export LIGHTGREY='\033[0;37m'

export DARKGREY='\033[1;30m'
export LIGHTRED='\033[1;31m'
export LIGHTGREEN='\033[1;32m'
export YELLOW='\033[1;33m'
export LIGHTBLUE='\033[1;34m'
export LIGHTPURPLE='\033[1;35m'
export LIGHTCYAN='\033[1;36m'
export WHITE='\033[1;37m'

export DEFAULTCOLOR='\033[0m'
export BOLD='\033[1m'

If it helps, I got the osascript from here

2

The problem are the single quotes in osascript -e '...'. Single quotes prevent variable expansion. This means that ${2} remains ${2} instead of being replaced with Spotify for example.

Try this:

function addtologin(){
    if [ $3 = "hidden" ]; then
        osascript -e "tell application \"System Events\" to make new login item with properties {path:\"${1}/${2}.app\", name:\"${2}\", hidden:true}"
    else
        osascript -e "tell application \"System Events\" to make new login item with properties {path:\"${1}/${2}.app\", name:\"${2}\", hidden:false}" 
    fi
}

Here we use double quotes which allow for expansion. To not confuse them with the inner quotes we need to escape those with \.

Now running the command with Spotify will output login item Spotify. This is just the result from the AppleScript, a login item was created. To suppress this you can add >/dev/null to the command: osascript -e "tell ..." >/dev/null.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .