如何自动下载并安装 OS X 应用程序?

如何自动下载并安装 OS X 应用程序?

iusethis.com重定向 URL,例如http://osx.iusethis.com/app/download/appname下载链接,你可以curl Mac更新页面提供直接下载链接。有没有更完整或更可靠的下载链接来源?

如何自动提取不同类型的档案或运行安装程序?拖放区附带一个脚本,该脚本接受 dmg 或 zip 文件作为输入,并将其中的应用程序包移动到/Applications/。有谁知道可以与安装程序或更多文件类型一起使用的类似脚本吗?

答案1

我最终将这个 shell 脚本拼凑起来,但停止了维护,因为 iusethis.org 上的许多下载链接不再有效,而且现在有更多应用程序通过 App Store 分发。无论如何,我还是把它留在这里作为记录。

#!/bin/bash

export uuid1=$(uuidgen) uuid2=$(uuidgen)
export tmp1=/tmp/autoapp$uuid1 tmp2=/tmp/autoapp$uuid2
mkdir -p $tmp1 $tmp2

shopt -s nullglob
IFS=$'\n'

trap 'onexit &' EXIT
onexit() {
  for mp in $tmp1/mountpoint-*; do
    hdiutil detach -quiet -force $mp
  done
  rm -rf /tmp/autoapp$uuid1 /tmp/autoapp$uuid2
}

app() {
  basename="$(basename "$1")"
  ditto "$1" "/Applications/$basename" 2> /dev/null
  echo "Copied $basename to /Applications/"
}

prefpane() {
  basename="$(basename "$1")"
  ditto "$1" ~/"Library/PreferencePanes/$basename" 2> /dev/null
  echo "Copied $basename to PreferencePanes/"
}

wdgt() {
  basename="$(basename "$1")"
  ditto "$1" ~/"Library/Widgets/$basename" 2> /dev/null
  echo "Copied $basename to Widgets/" 
}

pkg() {
  basename="$(basename "$1")"
  sudo installer -pkg "$1" -target /
  if [[ $? != 0 ]]; then
    read -n 1 -p "Open $basename (y/n)? " answer
    echo
    [[ "$aswer" == y ]] && open "$f" && exit 1
  fi
}

dmg() {
  mp=$tmp1/mountpoint-$(uuidgen)
  basename="$(basename "$1")"
  echo "Mounting $basename"
  yes | hdiutil attach -noverify -nobrowse -mountpoint $mp "$1" > /dev/null
  getfound "$mp" && exit
  echo "Couldn't find an application or package inside $basename"
  exit 1
}

gettmp() {
  getfound $tmp2 && exit

  found="$(find $tmp2 -depth 1 -name "*.dmg" -print0 2> /dev/null |
  xargs -0 du -s | sort -n | tail -n 1 | cut -f 2)"
  [[ -n "$found" ]] && dmg "$found" && exit

  echo "Couldn't find an application or package"
}

getfound() {
  found="$(find "$1" -maxdepth 2 -name "*.app" -print0\
  -o -name "*.prefPane" -print0 -o -name "*.pkg" -print0\
  -o -name "*.mpkg" -print0 2> /dev/null |
  xargs -0 du -s | sort -n | tail -n 1 | cut -f 2)"
  if [[ "$found" == *.app ]]; then
    app "$found"
    exit
  elif [[ "$found" == *.prefPane ]]; then
    prefpane "$found"
    exit
  elif [[ "$found" == *.wdgt ]]; then
    wdgt "$found"
    exit
  elif [[ "$found" == *.safariextz ]]; then
    open "$found" && exit
  elif [[ "$found" == *.pkg || "$found" == *.mpkg ]]; then
    pkg "$found"
    exit
  fi
  exit 1
}

untradedouble() {
  echo -n "$1" | ruby -e 'require "CGI"
  x = $<.read
  if x =~ /^http:\/\/.*?tradedoubler.com.*?&url=(.+)/
    print CGI.unescape($1)
  else
    print x
  end'
}

[[ $# == 0 ]] && exit
[[ "$1" == "-h" || "$1" == "--help" || "$1" == "-help" ]] && exit

if [[ -e "$@" ]]; then
  [[ -e "$@" ]] && appfile="$@" || exit
elif [[ "$@" =~ "/" || "$@" =~ "." ]]; then
    echo "The file $@ does not exist"
    exit 1
else
  download=true
fi

if [[ $download ]]; then
  app="$@"
  app2="$(echo "$app" | tr '[:upper:]' '[:lower:]' | sed 's|[^a-z0-9\-]||g')"
  iut=http://osx.iusethis.com/app/download/
  head="$(curl --head --max-time 5 -s -w "%{url_effective}\n" -L "$iut$app2")"
  ok="$(echo "$head" | grep "HTTP/1.[01] [23]..")"
  redirect="$(echo "$head" | tail -n 1 | tr -d '\n')"
  bytes=$(echo "$head" | sed -n 's|Content-Length: ||p' |
  tail -n 1 | tr -d '\r')
  if [[ -z "$redirect" || -z "$ok" ]]; then
    read -n 1 -p "Couldn't find $app. Search with Google (y/n)? " answer
    echo
    google="http://www.google.com/search?q=mac+application+"
    [[ $answer =~ [y|Y] ]] && open "$google$app2"
    exit
  fi
  ext="${redirect##*.}"
  exts=" zip gz rar mpkg dmg safariextz prefPane "
  if [[ "$exts" != *" $ext "* || "$bytes" -le 5000 ]]; then
    redirect="$(untradedouble "$redirect")"
    echo "Redirected to $redirect"
    read -n 1 -p "Open in a browser (y/n)? " answer
    echo
    [[ $answer == "y" || $answer == "Y" ]] && open "$redirect"
    exit
  fi
  appfile="$tmp1/${redirect##*/}"
  echo "Downloading $redirect"
  curl "$redirect" -o "$appfile"
fi

appfile="$(echo -n "$appfile" | sed 's|/$||')"

if [[ "$appfile" == *.app ]]; then
  app "$appfile"
elif [[ "$appfile" == *.prefPane ]]; then
  prefpane "$appfile"
elif [[ "$appfile" == *.wdgt ]]; then
  wdgt "$appfile"
elif [[ "$appfile" == *.safariextz ]]; then
  open "$appfile"
elif [[ "$appfile" == *.zip ]]; then
  unzip -q "$appfile" -d $tmp2
  gettmp
elif [[ "$appfile" == *.tar.gz ]]; then
  tar -xf $appfile -C $tmp2
  gettmp
elif [[ "$appfile" == *.dmg ]]; then
  dmg "$appfile"
elif [[ "$appfile" == *.pkg || "$appfile" == *.mpkg ]]; then
  pkg "$appfile"
else
  echo "Couldn't process $(basename "$appfile")"
  exit
fi

答案2

看看 AppFresh。如果您将应用程序输入到您的 iUseThis 配置文件中,您应该能够使用该工具的半自动下载过程下载它们。只需从侧边栏中选择“已使用但未安装”(或类似内容)即可。


由于您似乎对自动处理dmg文件感兴趣,请尝试以下操作:

#!/usr/bin/env bash
tempimgfile=$( mktemp img.img) || exit 1
mountpoint=$( mktemp -d img.mnt) || exit 1
curl "$1" > ${tempimgfile} || exit 1
hdiutil attach -mountpoint ${mountpoint} ${tempimgfile} || exit 1
cp -r ${mountpoint}/*.app "/Applications"
hdiutil unmount ${mountpoint}
rm -rf ${mountpoint}
rm -f ${tempimgfile}

使用方式如下:

$ ./unpack.sh http://www.panic.com/stattoo/d/Stattoo%201.5.dmg

当然,它只适用于安装新应用程序,不进行任何错误处理,因此使用时请自担风险。但打开dmg文件并不是真正的挑战——拥有一个全面的下载位置数据库才是。

答案3

以下是一些自动安装.dmg软件包的其他选项:

  1. 芽菜裹
  2. 独奏者+厨师
  3. 自制+酒桶

Soloist + sprout-wrap 厨师食谱

前两个选项允许您利用厨师自动将软件安装到 Mac(或其他操作系统)上的秘诀。芽菜裹Pivotal Labs 的项目是一系列食谱和配方,为 Mac OS X 提供软件安装配方。 独奏者红宝石这使得它非常容易运行chef-solo通过提供一个文件来包含run_list要运行的食谱/菜谱以及attributes菜谱使用的变量(如变量)。

自制食谱有一个homebrew_cask提供者sprout-osx-apps 菜谱sprout-osx-base::homebrew 配方使用安装自制酒桶brew cask install。要使用sprout-osx-base::homebrew配方,只需将要安装的酒桶名称添加到节点属性下node[:sprout][:homebrew][:casks] 在你的 soloistrc 中就像这个例子中的那样

为了简单起见,只需查看sprout-wrap 的自述文件并按照安装步骤操作。如果您想自定义soloistrc文件以选择要安装的内容,可以查看Pivotal 的示例在这里或者看看我的这里有更高级和定制的版本

如果你想要一种更简单的入门方式,请查看独奏巫师。它将生成一个自动安装引导脚本供您开始使用。

自酿啤酒桶

如果你不想找任何可定制的东西,或者认为 Chef/soloist 太复杂……那就从自酿啤酒桶并检查您的包裹是否已有现成的木桶配方brew cask search

相关内容