【问题标题】:Need a bash script to automate the annoying downloaded .DMG application install process需要一个 bash 脚本来自动化烦人的下载 .DMG 应用程序安装过程
【发布时间】:2011-09-05 10:07:57
【问题描述】:

我正在尝试将 .DMG 文件附带的繁琐应用程序安装过程自动化。

我想要一个 bash 脚本:

  1. 将上次下载的 .dmg 文件挂载到 ~/Downloads 目录中。
  2. 将其中的 .app 文件复制到 /Applications 目录。
  3. 卸载并删除上述 .DMG 文件。

我几乎是一个 bash 脚本新手,但我认为这个脚本的好处应该是显而易见的。我环顾了谷歌并没有找到解决这个问题的方法,这很遗憾。让我们做一个。

This answer 提供了一个很好的开端,但还不够自动化。

【问题讨论】:

    标签: macos bash automation


    【解决方案1】:

    你想念什么?我认为只缺少一件事,寻找应用程序并将其复制到/Application/ 目录。

    MOUNTPOINT="/Volumes/MountPoint"
    IFS="
    "
    hdiutil attach -mountpoint $MOUNTPOINT <filename.dmg>
    
    for app in `find $MOUNTPOINT -type d -maxdepth 2 -name \*.app `; do
      cp -a "$app" /Applications/
    done
    
    hdiutil detach $MOUNTPOINT
    

    当然,您需要将&lt;filename.dmg&gt; 设置为 dmg 文件的目标位置。这可以是第一个参数或类似的东西。

    【讨论】:

    • 我认为文件名/路径应该是一个可选参数。理想情况下,它会在 ~/Downloads 中找到最新的 .dmg 文件。
    • 然后查找带有find 的最新dmg 文件和提供的代码。
    • 或者你可以使用ls -rt ~/Downloads/*.dmg | tail -1
    【解决方案2】:

    我刚刚创建了一个很好的 ruby​​ 脚本。

    #!/usr/bin/env ruby
    
    require 'fileutils'
    require 'pathname'
    include FileUtils
    
    #go to downloads directory
    puts "installing most recent .dmg"
    cd File.expand_path("~/Downloads/")
    path = Pathname.new('.')
    
    #find most recent .dmg file
    files = path.entries.collect { |file| path+file }.sort { |file1,file2| file1.ctime <=> file2.ctime }
    files.reject! { |file| ((file.file? and file.to_s.include?(".dmg")) ? false : true) }
    last_dmg = files.last
    
    #if there is no .dmg file then reject this.
    if !last_dmg
      puts "No DMG files" 
      exit
    end
    
    puts "Mounting #{last_dmg}"
    
    mount_point = Pathname.new "/Volumes/#{last_dmg}"
    
    result = `hdiutil attach -mountpoint #{mount_point}  #{last_dmg}`
    
    #find any apps in the mounted dmg
    files = mount_point.entries.collect { |file| mount_point+file }
    files.reject! { |file| ((file.to_s.include?(".app")) ? false : true) }
    
    files.each { |app| 
      puts "Copying #{app} to Applications folder"
      `cp -a #{app} /Applications/` 
    }
    
    #unmount the .dmg
    puts "Unmounting #{last_dmg}"
    result = `hdiutil detach #{mount_point}`
    puts "Finished installing #{last_dmg}"
    
    
    #delete the .dmg
    rm last_dmg 
    

    这是一个很棒的 Alfred 扩展 http://cl.ly/391x0A0D0l2q0s0t3z2f

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多