最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

macos - Embedding AppleScript in Bash Script - Stack Overflow

programmeradmin4浏览0评论

I have a working AppleScript for pinning a given app the dock:

set theAppPath to "/Applications/Adobe Acrobat Reader.app"
try
    set theShellScript to "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & theAppPath & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
    do shell script theShellScript
    do shell script "killall -HUP Dock"
end try

but when I try to execute this inside a shell script using osascript it does nothing, here is the bash script:

#!/bin/bash
#!/usr/bin/env osascript
osascript <<EOD
set theAppPath to "/Applications/Adobe Acrobat Reader.app"
try
    set theShellScript to "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & theAppPath & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
    do shell script theShellScript
    do shell script "killall -HUP Dock"
end try
EOD
exit 0

I've looked at a number of questions that explain this and my format looks correct, I was able to use osascript to create a shortcut in another part of the bash script, but am having issues embedding this AppleScript snippet into the bash script.

I am going to try using an external AppleScript (.scpt) and calling that with the bash, but would prefer not to have to.

This will probably be a separate question if needed, but ideally my bash script would use a variable $APP_PATH inside of the osascript instead of theAppPath.

PS. MacOS 15

I have tried running the AppleScript directly and it works, I have tried using osascript multiple ways to call the AppleScript commands and none work. I was successful with a different osascript implementation but can't figure out why this one doesn't work.

EDIT: Here is the snippet of working osascript with its embedded AppleScript commands, note that this one already uses variables declared outside of the osascript component and they work from my limited testing

osascript <<EOF
tell application "Finder"
    make alias file to POSIX file "$APP_PATH/$APP_NAME" at POSIX file "$TARGET_FOLDER"
    set name of result to "$SHORTCUT_NAME"
end tell
EOF

I have a working AppleScript for pinning a given app the dock:

set theAppPath to "/Applications/Adobe Acrobat Reader.app"
try
    set theShellScript to "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & theAppPath & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
    do shell script theShellScript
    do shell script "killall -HUP Dock"
end try

but when I try to execute this inside a shell script using osascript it does nothing, here is the bash script:

#!/bin/bash
#!/usr/bin/env osascript
osascript <<EOD
set theAppPath to "/Applications/Adobe Acrobat Reader.app"
try
    set theShellScript to "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & theAppPath & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
    do shell script theShellScript
    do shell script "killall -HUP Dock"
end try
EOD
exit 0

I've looked at a number of questions that explain this and my format looks correct, I was able to use osascript to create a shortcut in another part of the bash script, but am having issues embedding this AppleScript snippet into the bash script.

I am going to try using an external AppleScript (.scpt) and calling that with the bash, but would prefer not to have to.

This will probably be a separate question if needed, but ideally my bash script would use a variable $APP_PATH inside of the osascript instead of theAppPath.

PS. MacOS 15

I have tried running the AppleScript directly and it works, I have tried using osascript multiple ways to call the AppleScript commands and none work. I was successful with a different osascript implementation but can't figure out why this one doesn't work.

EDIT: Here is the snippet of working osascript with its embedded AppleScript commands, note that this one already uses variables declared outside of the osascript component and they work from my limited testing

osascript <<EOF
tell application "Finder"
    make alias file to POSIX file "$APP_PATH/$APP_NAME" at POSIX file "$TARGET_FOLDER"
    set name of result to "$SHORTCUT_NAME"
end tell
EOF
Share Improve this question edited Mar 21 at 0:50 Daniel T.S asked Mar 20 at 23:17 Daniel T.SDaniel T.S 12 bronze badges 7
  • I was successful with a different osascript - what did that script look like? can you post a small, workable version in the question? – markp-fuso Commented Mar 20 at 23:46
  • 3 Is there a particular reason for using a shell script to run an AppleScript that runs shell scripts? – red_menace Commented Mar 20 at 23:59
  • @red_menace this is just a shell script that I want to embed AppleScript commands in, I mentioned instead of embedding the AppleScript commands I could save them as an external file and call them. That would not be ideal as I am trying to create a template in a single file for my coworkers to use when packaging apps for touchless Intune deployment. – Daniel T.S Commented Mar 21 at 0:45
  • @markp-fuso I will edit and add the section of the bash script with the working osascript / embedded AppleScript commands – Daniel T.S Commented Mar 21 at 0:46
  • @red_menace, after reviewing more I can see the areas in my AppleScripts that are in fact just calling shell commands. Honest answer is I am not sure, I am reviewing now and testing. Just to be clear, my goal is a bash script to pin an app to the dock after installing it. – Daniel T.S Commented Mar 21 at 1:07
 |  Show 2 more comments

1 Answer 1

Reset to default 0

I found my issue and it was actually caused by running as sudo vs the user. When I was executing the AppleScript in the script editor it was running as my user account and editing the com.apple.dock.plist in my /User/.... folder. This has the intended behaviour of editing my dock. When I ran the same code, osascript or bash directly, as sudo it did not edit the com.apple.dock.plist in either my user directory or the system directory /Library/Preferences/.

I am now trying to use PlistBuddy to explicitly edit the file that I want, taking some time to learn today

发布评论

评论列表(0)

  1. 暂无评论