Portable Feed source code

Home page > OS X Portable Applications > Portable Feed source code

Source code index | Portable Feed script code:


Shell Script (Release 1.0)

Portable Feed.app is packaged using the Platypus script wrapper from http://sveinbjorn.sytes.net/platypus

#!/bin/sh

##########################################################################
#
#   Portable Feed
#
#   $Revision: 1.0
#
#   The Contents of this file are made available subject to the terms of
#   either of the following licenses
#
#          - GNU General Public License Version 2.1
#
#   Carlo Gandolfi, Paolo Portabluri, May 2006
#
#   GNU General Public License Version 2.1
#   =============================================
#   Copyright 2006 by:
#      Carlo Gandolfi - http://www.freesmug.org
#      Paolo Portaluri - http://plus2.it/~paolo/
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public
#   License version 2.1, as published by the Free Software Foundation.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this library; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#   MA  02111-1307  USA
#
##########################################################################

# Reset PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

appurl=net.keeto.feed.plist
appid=Feed
userplistbase="Library/Preferences"
userplist="$HOME/$userplistbase/$appurl"
userprefbase="Library"
userpref="$HOME/$userprefbase/$appid"

p_userbase="$1/Contents/Resources/app"
p_userplist="$p_userbase/$userplistbase/$appurl"
p_userpref="$p_userbase/$userprefbase/$appid"
copy_pref="$p_userbase/CopyPref_Done"

# ========================================================
# quitapp
# Check if local app is open and quit it
# ========================================================

function quitapp {
if ps cx | grep '[0-9] '$appid'$' > /dev/null; then
message1="A copy $appid is already running. Do you want to quit the running application and open Portable $appid instead?"
button=`/usr/bin/osascript << EOT
    tell application "Finder"
    beep
    display dialog "$message1" buttons {"Cancel", "Continue"} \
        with title "Qiut Running $appid" with icon caution default button "Continue"
    set result to button returned of result
    end tell
EOT`
if test "$button" == "Continue"; then                # Quit local application
    osascript -e 'tell app "'$appid'" to quit'
else                            
    exit 0
fi
fi
}

# ========================================================
# repair
# Repair preferences if Portable Application crashed
# ========================================================

function repair {
    if test -f "$userplist.pabu" || test -d "$userpref.pabu" ; then
message2="Portable $appid has crashed without restoring existing preferences. \
Do you want to restore the original preferences now \
or copy to BackUp folder to manually restore later?"
button=`/usr/bin/osascript << EOT
    tell application "Finder"
    beep
    display dialog "$message2" buttons {"Continue", "Restore", "Copy"} \
with title "Restoring Existing Preferences" \
with icon stop default button "Copy"
    set result to button returned of result
    end tell
EOT`
if test "$button" == "Copy"; then    # Save old preferences in BackUp folder
    rm -rf "$HOME/$appid Pref BackUp/"
    mkdir -p "$HOME/$appid Pref BackUp/$userplistbase"
    mkdir -p "$HOME/$appid Pref BackUp/$userprefbase"
    mv "$userplist.pabu" "$HOME/$appid Pref BackUp/$userplistbase/$appurl.plist"
    mv "$userpref.pabu" "$HOME/$appid Pref BackUp/$userprefbase/$appid"
elif test "$button" == "Restore"; then    # Restore existing preference
    recover_plist
    rm -rf "$userpref"
    recover_asf
else
    rm -rf "$userpref.pabu"
    rm -f "$userplist.pabu"
fi
fi
}

# ========================================================
# check_p_asf
# Check and create Portable Application Support folders
# ========================================================

function check_p_asf {
    if [ ! -d "$p_userpref" ]; then
        mkdir -p "$p_userpref"
    fi
    if [ ! -d "$p_userbase/$userplistbase" ]; then
        mkdir -p "$p_userbase/$userplistbase"
    fi
}

# ========================================================
# copy_local_pref
# Copy local preferences to Portable Application
# ========================================================

function check_asf {
    if [ -d "$p_userpref" ]; then
        rm -Rf "$p_userpref"
    else
        mkdir -p "$p_userpref"
    fi
}

# copy Preferences Folder
function copy_pf {
userprefsize=`du -hc $userpref | cut -f1 | tail -1`
message4="Your $appid Preferences folder is $userprefsize. Copy it to Portable $appid?"
button=`/usr/bin/osascript << EOT
    tell application "Finder"
    beep
    display dialog "$message4" buttons {"Copy", "Cancel"} \
    with title "Copy $appid Preferences Folder" \
    with icon note default button "Cancel"
    set result to button returned of result
    end tell
EOT`
if test "$button" == "Copy"; then
    check_asf
    bu_p_plist
    cp -R "$userpref" "$p_userpref"
    touch -f "$copy_pref";
fi
}

function copy_local_pref {
if [ ! -f "$copy_pref" ] && test -d  "$userpref" ; then
message="Copy the existing $appid preferences on this system to Portable $appid?"
button=`/usr/bin/osascript << EOT
    tell application "Finder"
    beep
    display dialog "$message" buttons {"Never", "Copy", "Don't Copy"} \
    with title "Copy Preferences" \
    with icon note default button "Don't Copy"
    set result to button returned of result
    end tell
EOT`
if test "$button" == "Copy"; then
    copy_pf    
elif test "$button" == "Never"; then
    touch -f "$copy_pref"; 
fi
fi
}

# ========================================================
# bu/recover preference
# BackUp existing preference and restore portable ones
# ========================================================

function bu_plist {
    mv "$userplist" "$userplist.pabu"
}

function recover_plist {
    mv "$userplist.pabu" "$userplist"
}

function recover_p_plist {
    cp "$p_userplist" "$userplist"
}

function bu_p_plist {
    cp "$userplist" "$p_userplist"
}

# ========================================================
# bu/recover asf
# BackUp existing Application Support Folder and restore portable ones
# ========================================================

function bu_asf {
    mv "$userpref" "$userpref.pabu"
}

function recover_asf {
    mv "$userpref.pabu" "$userpref"
}

function recover_p_asf {
    ln -s "$p_userpref" "$userpref"
}

function bu_p_asf {
    rm -rf "$userpref"
}

# ========================================================
# run
# Open Portable Application
# ========================================================

function run_app {
    here="`dirname \"$0\"`"
    cd "$here"
    "$here/app/$appid.app/Contents/MacOS/$appid"
}

# ========================================================
# open_p_app
# Run Portable Application script
# ========================================================

function open_p_app {
    if  test -f "$userplist" ; then
        bu_plist        # Backup of existing preference
    fi
    if test -d "$userpref" ; then
        bu_asf            # Backup existing Applicatin Support folder
    fi
    recover_p_plist            # Recover portable preference
    recover_p_asf            # Recover portable Applicatin Support folder
    run_app             # Run application from external drive
    bu_p_plist            # Backup portable preference
    bu_p_asf            # Backup portable Applicatin Support folder
    if  test -f "$userplist.pabu" ; then
        recover_plist        # Restore existing preference
    else                # Remove portable preference
        rm -f "$userplist"
    fi
    if test -d "$userpref.pabu"; then
        recover_asf        # Restore portable Application Support folder
    fi
}

quitapp
repair
check_p_asf
copy_local_pref
open_p_app

EOF
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License