Try-Fix #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test Build VBA (macOS) | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- mac | |
permissions: | |
id-token: write | |
attestations: write | |
jobs: | |
build: | |
runs-on: macos-latest | |
steps: | |
- name: "Checkout" | |
uses: actions/checkout@v4 | |
- name: "Install Office" | |
run: | | |
# macOS Office installers: https://github.com/alsyundawy/Microsoft-Office-For-MacOS | |
# Download the Office installer from: https://go.microsoft.com/fwlink/?linkid=525133 | |
curl -L "https://go.microsoft.com/fwlink/?linkid=525133" -o microsoft_office_installer.pkg | |
# Debug: print current directory and list files | |
pwd | |
ls -la | |
# Install Office without trying to use choices.xml | |
# The standard installer should work in headless mode in GitHub Actions | |
sudo installer -pkg microsoft_office_installer.pkg -target / | |
# Disable Microsoft AutoUpdate after installation | |
defaults write com.microsoft.autoupdate2 SendAllTelemetryEnabled -bool FALSE | |
defaults write com.microsoft.autoupdate2 DisableInsiderCheckbox -bool TRUE | |
defaults write com.microsoft.autoupdate2 HowToCheck -string "Manual" | |
shell: bash | |
- name: "Configure AppleScript Permissions" | |
run: | | |
# Alternative approach using privacy settings directly | |
# Try to enable automation permissions using tccutil (may require Full Disk Access) | |
sudo sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db "SELECT * FROM access LIMIT 1" || echo "Failed to query TCC database, may need different approach" | |
# Grant Screen Recording permission (needed for screenshots and sometimes for automation) | |
sudo sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db "INSERT OR REPLACE INTO access VALUES('kTCCServiceScreenCapture','com.apple.screencapture',1,1,1,NULL,NULL,NULL,'UNUSED',NULL,0,1.0,NULL,NULL,NULL,0,NULL,NULL,NULL,'UNUSED',NULL,0,0,0,NULL);" 2>/dev/null || echo "Failed to add screen capture permission" | |
# Enable Accessibility features (alternative approach) | |
sudo touch /private/var/db/.AccessibilityAPIEnabled | |
# Create a UI script to click "Allow" button on permission dialogs | |
cat > /tmp/click_allow.applescript << 'EOF' | |
on run argv | |
try | |
tell application "System Events" | |
set procName to item 1 of argv | |
tell process procName | |
-- Look for Allow button on dialog | |
repeat 30 times | |
if exists button "Allow" of window 1 then | |
click button "Allow" of window 1 | |
exit repeat | |
end if | |
delay 0.5 | |
end repeat | |
end tell | |
end tell | |
return "UI Script succeeded" | |
on error errMsg | |
return "UI Script error: " & errMsg | |
end try | |
end run | |
EOF | |
# Compile the script | |
osacompile -o /tmp/click_allow.scpt /tmp/click_allow.applescript | |
# Set necessary permissions | |
chmod +x "$GITHUB_WORKSPACE/scripts/mac/run.applescript" | |
chmod +x /tmp/click_allow.scpt | |
shell: bash | |
- name: "Pre-open Excel" | |
run: | | |
# Launch Excel before running the macro | |
open -a "Microsoft Excel" | |
# Start the UI automation script to click Allow in background | |
osascript /tmp/click_allow.scpt "Microsoft Excel" & | |
# Give Excel time to initialize | |
sleep 15 | |
# Take a screenshot to see if Excel opened correctly | |
mkdir -p screenshots | |
screencapture -x screenshots/excel_opened.png | |
shell: bash | |
- name: "Run Excel Macro" | |
timeout-minutes: 5 | |
run: | | |
# Debug: Show current directory and list contents of key directories | |
pwd | |
ls -la | |
ls -la tests/fixtures || echo "fixtures directory not found" | |
# Use repo-relative paths since we're in the repo root after checkout | |
WORKBOOK_PATH="./tests/fixtures/ExcelForMacWorkbook.xlsm" | |
APPLESCRIPT_PATH="./scripts/mac/run.applescript" | |
# Make sure the workbook exists | |
if [ ! -f "$WORKBOOK_PATH" ]; then | |
echo "Error: Excel workbook not found at $WORKBOOK_PATH" | |
ls -la "$(dirname "$WORKBOOK_PATH")" | |
exit 1 | |
fi | |
# Make sure the AppleScript exists | |
if [ ! -f "$APPLESCRIPT_PATH" ]; then | |
echo "Error: AppleScript not found at $APPLESCRIPT_PATH" | |
exit 1 | |
fi | |
# Get absolute paths for files (AppleScript needs absolute paths) | |
ABS_WORKBOOK_PATH="$(pwd)/$WORKBOOK_PATH" | |
ABS_APPLESCRIPT_PATH="$(pwd)/$APPLESCRIPT_PATH" | |
echo "Using workbook: $ABS_WORKBOOK_PATH" | |
echo "Using script: $ABS_APPLESCRIPT_PATH" | |
# Start the UI automation script to click Allow in background again | |
osascript /tmp/click_allow.scpt "Microsoft Excel" & | |
# Run the AppleScript with a time limit using perl instead of timeout | |
# This will kill the process after 60 seconds if it hangs | |
perl -e ' | |
alarm(60); | |
system("osascript \"$ARGV[0]\" excel \"$ARGV[1]\" \"$ARGV[2]\""); | |
if ($? != 0) { | |
print "AppleScript timed out or failed, checking for output file anyway\n"; | |
} | |
' "$ABS_APPLESCRIPT_PATH" "$ABS_WORKBOOK_PATH" "WriteToFile" | |
# Display the output file to confirm the macro executed successfully | |
OUTPUT_DIR="$(dirname "$ABS_WORKBOOK_PATH")" | |
OUTPUT_FILE="$OUTPUT_DIR/ExcelForMacWorkbook.txt" | |
echo "Checking if output file was created:" | |
ls -l "$OUTPUT_FILE" || echo "Output file not found" | |
if [ -f "$OUTPUT_FILE" ]; then | |
echo "Content of output file:" | |
cat "$OUTPUT_FILE" | |
fi | |
# If the AppleScript method failed, try a fallback direct VBA execution | |
if [ ! -f "$OUTPUT_FILE" ]; then | |
echo "Trying fallback method to run VBA macro directly..." | |
# Create a JavaScript file to execute the macro directly | |
cat > /tmp/run_excel_macro.js << 'EOF' | |
var app = Application('Microsoft Excel'); | |
app.includeStandardAdditions = true; | |
app.activate(); | |
try { | |
var wb = app.open(arguments[0]); | |
delay(2); | |
app.runVisualBasicMacro(arguments[1]); | |
delay(2); | |
console.log("JavaScript macro execution completed"); | |
} catch(e) { | |
console.log("Error: " + e); | |
} | |
EOF | |
# Run the JavaScript | |
osascript -l JavaScript /tmp/run_excel_macro.js "$ABS_WORKBOOK_PATH" "WriteToFile" | |
# Check if output file was created after fallback | |
echo "Checking if output file was created after fallback:" | |
ls -l "$OUTPUT_FILE" || echo "Output file not found after fallback" | |
if [ -f "$OUTPUT_FILE" ]; then | |
echo "Content of output file after fallback:" | |
cat "$OUTPUT_FILE" | |
fi | |
fi | |
shell: bash | |
- name: "Take exiting screenshot" | |
if: always() | |
run: | | |
mkdir -p screenshots | |
screencapture -x screenshots/$(date +%Y%m%d_%H%M%S).png | |
shell: bash | |
- name: "Upload Screenshots" | |
if: always() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: "Screenshots" | |
path: "./screenshots/*" | |
if-no-files-found: warn | |
- name: "Upload Output Files" | |
if: always() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: "MacroOutput" | |
path: "./tests/fixtures/*.txt" | |
if-no-files-found: warn |