AppleScript and Fastlane - applescript

How can I get AppleScript to run fastlane gym command?
set pathToWorkspace to choose folder with prompt "Select Your Workspace"
do shell script "cd " & pathToWorkspace's POSIX path's quoted form
do shell script "fastlane gym"
It throws an error - command not found
Googleing about I found that I need to add the actual path for the command but I am a noob at scripting and not sure what what the path would be. Any help would be appreciated.

Related

Running a Service with sudo administrator privileges

I have the following shell script that I normally run successfully as a system service (created in automator:
#!/bin/bash
for f in "$#"
do ln -s "$f" "${f}.deleteThisExtensionAfterMoving"
echo "${f}.deleteThisExtensionAfterMoving"
done
However, when trying to use this to make symlinks in folders that I'm not the owner of, it fails.
I tried saving it as a script, then using applescript in automator as I've seen described elsewhere here:
on run {input, parameters}
do shell script ("sudo ~/Documents/z_misc/makesymlink '" & POSIX path of input & "'") with administrator privileges
end run
However, upon running the service, I see a "command not found" error.
When I tried saving the script as a .sh file and using that instead, it still didn't work. When I try running from terminal without the preceding "sudo" command, I get a "permission denied" error.
When I try running from terminal without the preceding "sudo" command, I get a "permission denied" error.
The "permission denied" error when attempting to execute a shell script (or binary executable) is typically caused because the file is not set as executable. Use the following command in Terminal:
chmod +x file
Or:
chmod +x /path/to/file
Quoting if there are spaces or escaping spaces with a backslash (\), but not both.
However, the "permission denied" error can also be caused if there is a privileges issue with where the shell script or binary executable is located.
Keeping executables in you Documents folder or any of the default folders, except for Public, within your Home folder can be problematic to Automator actions and for use in Terminal.
I created a bin folder in my Home folder and place my shell scripts and binary executables that I do not want elsewhere, e.g. /use/local/bin, and have added "$HOME/bin" to my PATH.
Here is what I'd use in the Run AppleScript action in my Automator Service/Quick Action:
Example AppleScript code:
on run {input, parameters}
repeat with thisItem in input
set thisItem to the quoted form of the POSIX path of thisItem
do shell script ("$HOME/bin/makesymlink " & thisItem) with administrator privileges
end repeat
end run
As coded, this allows for multiple Finder items to be passed to the Automator Service/Quick Action.
That all said, without seeing the contest of the makesymlink file there is not anything else I can think off at the moment.
From the linked Technical Note below:
Note: Using sudo(8) with with administrator privileges is generally unnecessary and creates security holes; simply remove the sudo.
Have a look at Technical Note TN2065 do shell script in AppleScript
If you are going to use the do shell script command, then I suggest you read the entire Technical Note.

sudo cp command to copy into launchDaemons

Just simplyfing a process for some internal mac users to put files in the right places but having trouble with a plist file needing to go in /Library/LaunchDaemons. Using some applescript to accomplish this. Unfortunately (for me) LaunchDaemons is read only, so need to sudo root privilege to copy a file into there. I can't seem to get it right as to how to do that with applescript. Copying files to unrestricted locations is done just fine with something like
do shell script ("/bin/cp " & posix_path & "file_to_copy.crt" & "/path/to/folder/")
For the plist file in LaunchDaemons, ideally something like below would work,
do shell script ("sudo /bin/cp " & posix_path & "file_to_copy.plist" & "/Library/LaunchDaemons/") with administrator privileges
I've tried a lot of variations but no luck. Reading through stack overflow I haven't spotted a question quite like this. Any insight would be much appreciated!
As far as using sudo in a do shell script command, do not use sudo in a do shell script command, have a look at: Technical Note TN2065 do shell script in AppleScript
As far as /Library/LaunchDaemons/, it is not read-only! In macOS Big Sur, here are the permissions on a recent 11.4 build:
drwxr-xr-x 3 root wheel - 96 May 30 13:01 LaunchDaemons
Note that it is writable for root and why you need to use with administrator privileges when using the do shell script command to write to it.
Example AppleScript code
do shell script "cp '/path/to/filename.plist' '/Library/LaunchDaemons/'" with administrator privileges
The above do shell script command works for just fine me.
Note the single-quotes around the POSIX paths.
If you are not using hard coded POSIX paths and using variables and concatenating the do shell script command, you can use e.g, the quoted form of theFilename or theFilename's quoted form, etc.
Example AppleScript code
set theFilename to the POSIX path of (choose file)
set theDestinationFolder to "/Library/LaunchDaemons/"
do shell script "cp " & ¬
theFilename's quoted form & space & ¬
theDestinationFolder's quoted form ¬
with administrator privileges

New to applescript

Essentially, what I am trying to accomplish is I want to cd into a specific directory containing all my lighthouse folders. I want to loop through each line of urls.txt and for every URL execute the npm lighthouse command and output the results into a CSV to analyze later.
do shell script "cd /Users/user/Documents/Lighthouse\\"
set srcFile to "urls.txt"
set lns to paragraphs of (read file srcFile as «class utf8»)
repeat with ln in lns
do shell script "npm lighthouse --throttling-method simulate --verbose --view --emulated-form-factor mobile --output-path "/Users/user/Documents/Lighthouse/LighthouseReports.csv" https://www.aphrodites.com/collections/bestsellers-home/products/tree-of-life-heart-edition-charm-bracelet-with-real-austrian-crystals"
end repeat
Some help would be fantastic, thank you!
The following will probably get you a bit closer. At least, it's valid AppleScript for what I think you're trying to do (never having used lighthouse before). I'm assuming that you want that given URL in the do shell script to be the URL stored in each line of the file, right?
The quoted form of command makes sure that paths are properly quoted for unix, saving you some headaches. I've used the full path to npm, because do shell script doesn't import your interactive shell PATH variable and won't find that utility. if you ever want to know wether do shell script will be able to find a utility, run do shell script "which utilityname"; if the command errors out, use the full path.
set srcFolder to "/Users/user/Documents/Lighthouse/"
set srcFile to srcFolder & "urls.txt"
set outputPath to quoted form of (srcFolder & "LighthouseReports.csv")
set lns to paragraphs of (read file srcFile as «class utf8»)
repeat with ln in lns
do shell script "/usr/local/bin/npm lighthouse --throttling-method simulate --verbose --view --emulated-form-factor mobile --output-path " & outputPath & " " & quoted form of ln
end repeat

applescript does not find command line tool under /usr/local/bin

AppleScript does not find a command line tool located in /usr/local/bin/.
I've got the following AppleScript command:
do shell script "/usr/local/bin/bitbar refresh"
It results in the error:
error "env: node: No such file or directory" number 127
I checked the directory and the command line tool is indeed in that location. AppleScript does find other tolls installed there, such as brew.
Any ideas?
Try initially adding a new path (i.e. /usr/local/bin/) to the shells PATH variable before running the bitbar refresh command.
For instance, change your do shell script as follows:
do shell script "PATH=/usr/local/bin:$PATH; bitbar refresh"

Getting folder actions to run a shell script

I'm trying to to write an applescript that will run any shell script that is dropped on a particular folder on my network.
I've been trying with:
on adding folder items to target_folder after receiving this_script
tell application "Terminal" to open this_script
end adding folder items to
But I'm not having any luck.
Any ideas on what could be wrong?
Untested, but try:
on adding folder items to target_folder after receiving added_items do shell script " " end adding folder items to
See Technical Note TN2065: do shell script in AppleScript on how to format your shell script.
I'll bet cash money it's because you have some characters that need escaping in your shell script. Post your script.

Resources