简体   繁体   English

AppleScript do Shell脚本找不到Android ADB

[英]Applescript do shell script can't find Android ADB

I have a couple of shell scripts stored in the /Scripts folder of my AppleScript application. 我的AppleScript应用程序的/ Scripts文件夹中存储了几个Shell脚本。

I can access them by setting my base path 我可以通过设置基本路径来访问它们

set basePath to POSIX path of ((path to me as text)) & "Contents/Resources/Scripts/"

But I'm only able to run the script if I call the Terminal app 但是我只能在调用终端应用程序的情况下运行脚本

-- This works

tell application "Terminal"
    set currentTab to do script (basePath & "install_key.sh")
end tell

-- This does not work

do shell script basePath & "install_key.sh"

The error on do shell script complains about not being able to find adb (Android Debug Bridge) do shell script上的错误抱怨无法找到adb (Android调试桥)

FWIW, here is the shell script in question ( install_key.sh ) FWIW,这是有问题的外壳程序脚本( install_key.sh

#!/bin/bash

#Find script directory
DIR="$( cd "$( dirname "$0" )" && pwd )"

adb push $DIR"/key-dev.txt" /sdcard/ &&
adb shell mv /sdcard/key-dev.txt /sdcard/key.txt

The Technical Note TN2065: do shell script in AppleScript is the key reference for this kind of issues. TN2065技术说明:AppleScript中的shell脚本是此类问题的关键参考。

when you use just a command name instead of a complete path, the shell uses a list of directories (known as your PATH) to try and find the complete path to the command. 当您仅使用命令名称而不是完整路径时,Shell将使用目录列表(称为PATH)来尝试查找命令的完整路径。 For security and portability reasons, do shell script ignores the configuration files that an interactive shell would read, so you don't get the customizations you would have in Terminal. 出于安全性和可移植性的原因, shell脚本会忽略交互式外壳程序将读取的配置文件,因此不会获得终端机中的自定义设置。

First: find the full path of adb 第一:找到adb的完整路径

you have to open a Terminal and issue the following command: 您必须打开终端并发出以下命令:

$ which adb

suppose the response is: 假设响应为:

/Users/ronda/projects/android/sdk/platform-tools/adb

this means that the path of adb is: /Users/ronda/projects/android/sdk/platform-tools , now we have several way to address the problem, for example follow one of these two options: 这意味着adb的路径是: / Users / ronda / projects / android / sdk / platform-tools ,现在我们有几种方法可以解决此问题,例如,遵循以下两个选项之一:

Option1: Fix the AppleScript 选项1:修复AppleScript

do shell script "PATH=${PATH}:/Users/ronda/projects/android/sdk/platform-tools; export PATH; echo $PATH; " & basePath & "install_key.sh"

Option2: Fix the shell script 选项2:修复Shell脚本

for example you could specify full path to the adb command in your .sh this way: 例如,您可以通过以下方式在.sh中指定adb命令的完整路径:

#!/bin/bash

#Find script directory
DIR="$( cd "$( dirname "$0" )" && pwd )"

/Users/ronda/projects/android/sdk/platform-tools/adb push $DIR"/key-dev.txt" /sdcard/ &&
/Users/ronda/projects/android/sdk/platform-tools/adb shell mv /sdcard/key-dev.txt /sdcard/key.txt

The simplest solution would be running the same bash configuration as your terminal application. 最简单的解决方案是运行与终端应用程序相同的bash配置。 The main difference is that Terminal uses an interactive bash and do shell script command doesn't. 主要区别在于Terminal使用交互式bash,而do shell script命令则不使用。 To run an interactive shell you can simply execute a new one with option -i (stands for interactive). 要运行交互式外壳,您只需使用选项-i(代表交互式)即可执行一个新外壳。 When an interactive shell is opened the ~/.bashrc file is used, while non-interactive shells don't use this file. 当打开交互式外壳程序时,将使用〜/ .bashrc文件,而非交互式外壳程序则不使用此文件。

do shell script "bash -i <<<" & quoted form of (basePath & "install_key.sh" as text)

if you don't like that you can simply execute the bashrc file or read the path variable and set it in a do shell script. 如果您不喜欢它,则可以简单地执行bashrc文件或读取path变量并将其设置在do shell脚本中。

Problem 问题

If I understand correctly, your main issue is that your script cannot detect and hold the presence of a specific command located on a system. 如果我理解正确,那么您的主要问题是您的脚本无法检测并保留系统中特定命令的存在。

Solution

I believe the following code will be effective in helping you achieve your goal. 我相信以下代码将有效地帮助您实现目标。 This applescript allows you to find whether ADB is stored on a system and store it's path in a variable. 该小程序可让您查找ADB是否存储在系统上并将其路径存储在变量中。 You can add the variable to your path and export as others have suggested or have a look at the export process in Apple's TN2065 . 您可以将变量添加到路径中,然后按照其他人的建议进行导出,或者查看Apple TN2065中的导出过程。

If ADB is not found on a system then users can receive a prompt telling them what actions to take (if that aligns with your use-case or you could begin the install sequence for ADB). 如果在系统上未找到ADB,则用户会收到提示,告诉他们要采取什么措施(如果这符合您的用例,或者您可以开始ADB的安装过程)。 To test the behavior of the script you can simply change the adp to some other (fake) command that does not exist on your system. 要测试脚本的行为,您可以简单地将adp更改为系统上不存在的其他(伪)命令。 I've added the path to the dialog so that you can see the do shell is passing the contents of the which command into a variable. 我已将路径添加到对话框,以便您可以看到do shell正在将which命令的内容传递给变量。

try

    set adbPath to do shell script "which adb"

on error errStr number errorNumber

    -- If our own error number, warn about bad data.
    if the errorNumber is not equal to 0 then
        display dialog "ADB is not loaded onto system. Please load ADB and run this app again"
        return 0 -- Return the default value (0).
    else
        -- An unknown error occurred. Resignal, so the caller
        -- can handle it, or AppleScript can display the number.
        error errStr number errorNumber
    end if

end try

if length of adbPath > 0 then display dialog "ADB found continue processing..." & adbPath

The structure defined in the TN2065 above is essentially: 上面的TN2065中定义的结构实质上是:

$ VAR=something; export VAR $ osascript -e 'do shell script "echo $VAR"' something

You might also want to try the administrator option when calling the shell script: 您可能还想在调用Shell脚本时尝试使用管理员选项:

do shell script "command" user name "me" password "mypassword" with administrator privileges

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM