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
Related
I've got a folder being generated with today's date with another AppleScript script, and I'm now wanting to copy that folder with today's date to another hard drive. I'm very new to AppleScript and am trying to figure out how to copy a folder from one hard drive to another, given the file name changes every time the day changes.
The code I'm using to create the folder with a specific date is below.
tell application "System Events"
delay 0.5
do shell script "date +\"%m.%d.%y\""
keystroke result
delay 0.5
keystroke return
end tell
This script should address your basic request. It gets the current date and builds a source and destination path with it. It then uses the ditto command to copy the contents of the source directory to the corresponding destination directory. If the destination does not yet exist, it will create it. This should include any sub-directories as well.
use scripting additions
-- do shell script "date +\"%m.%d.%y\""
set date8 to do shell script "date +\"%Y.%m.%d\""
set srcRoot1 to path to desktop as text
set srcRoot2 to POSIX path of srcRoot1
--> /Users/username/Desktop/2022.10.04/
set destRoot1 to path to documents folder as text
set destRoot2 to POSIX path of destRoot1
--> /Users/username/Documents/2022.10.04
do shell script "ditto -V " & quoted form of (srcRoot2 & date8 & "/") & space & quoted form of (destRoot2 & date8) & " 2>&1"
--> "ditto -V '/Users/username/Desktop/2022.10.04/' '/Users/username/Documents/2022.10.04' 2>&1"
The results above show the shell command after it is resolved. There are two optional components here. The -V option prints a list of copied files to stderr. The closing 2>&1 sends stderr to the script result where it can be seen.
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
I want to run an Automator workflow from within an AppleScript. When I use Terminal I can do:
/usr/bin/automator -i /Users/.../filename /Users/.../worfklowname.workflow/
But when I do this as part of the AppleScript via
do shell script "/usr/bin/automator -i /Users/.../filename /Users/.../worfklowname.workflow/"
I get an error stating that Automator could not access the required data. What's wrong here?
Thank you!
I found a solution:
set startpath to "Macintosh HD:Users:...:filename"
set workflowpath to "Macintosh HD:Users:...:workflow"
set qtdstartpath to quoted form of (POSIX path of startpath)
set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
set command to "/usr/bin/automator -i " & qtdstartpath & " " & qtdworkflowpath
do shell script command
It's probably due to all the entanglements that this solution also looks a bit non-straightforward :-D
New to AppleScript but I'm trying to migrate over from terminal scripting. After much research I am having issues trying to get a shell script running from within the .app file.
What I have so far:
to the_foo()
tell application "Finder"
set current_path to container of (path to me) as alias
set path_in_posix to POSIX path of current_path
end tell
tell application "Terminal"
set new_terminal to do script " "
activate
do script "cd " & path_in_posix in window 1
do shell script "ls " & POSIX path of (path to me) & "Contents/Resources/Scripts/foobar.sh" in window 1
end tell
end the_foo
The error I am getting:
Learned to open a new terminal with: Applescript to open a NEW terminal window in current space
I added in window 1 when I learned that do script opens a new terminal window every time, referenced: applescript and terminal ( run several do shell script in one terminal window )
I originally tried:
set script_Location to path to resource "Contents:Resources:Scripts:"
set run_Script to (quoted form of script_Location) & "foobar.sh"
do shell script run_Script
after referencing: How to change AppleScript path to a Terminal-style path? but when I run it I get the same error.
So how can I run the shell script located within the Scripts folder within the same window 1? I would ideally like to set a variable for the path so I can put multiple shells scripts in the Scripts folder.
It's probably just a typo
do script "ls " & POSIX path of (path to me) & "Contents/Resources/Scripts/foobar.sh" in window 1
rather than
do shell script "ls " & ...
I recommend to use System Events instead of the Finder to get the container of the script
tell application "System Events"
set path_in_posix to POSIX path of container of (path to me)
end tell
Vadian had the correct and better approach in a one liner. I did change
"Contents/Resources/Scripts/foobar.sh"
to
set script_Location to "Contents/Resources/Scripts/"
set foobar to do script "bash " & POSIX path of (path to me) & script_Location & "foobar.sh" in window 1
this approach helps if I want to add more than one shell script in the Scripts folder.
I'm developing a Mac OS X application and in some case it needs to copy a file to /Library/ScriptingAdditions.
And using the code below
tell application "Finder"
duplicate sourcePath to destinationPath with replacing
end tell
will prompt a dialog saying "Finder" wants to make some changes...
I would like to make the dialog saying My Application wants to make some changes....
I've read about https://developer.apple.com/library/mac/documentation/security/conceptual/authorization_concepts/01introduction/introduction.html
but it doesn't seem to work with AppleScript.
If you would use the shell instead of AppleScript, the dialog asking for the password will display the name of your own application. Here's an example that copies the file "_this is a test.xyz".
set sourcePath to "'~/desktop/_this is a test.xyz' " -- mind extra space
set destPath to path to scripting additions folder -- change this to your destination folder
set destPath to POSIX path of destPath
set destPath to "'" & destPath & "_this is a test.xyz'"
set shellScript to "cp -n " & sourcePath & destPath
do shell script shellScript with administrator privileges
Warning: don't just run this script without modification, as it will add an empty file to your scripting additions folder and you probably don't want that. This script just serves as an example to look at.
Edit:
This will only work if you can compile your app as an independent app. If you're using Python, you need to compile your Python scripts as a standalone app with a name. The password dialog will show the name of the standalone app instead of "Python".