Skip to content
This repository has been archived by the owner on Jan 10, 2020. It is now read-only.

Commit

Permalink
Always load action scripts. Closes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Feb 16, 2019
1 parent 933f933 commit e8dba3d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 52 deletions.
13 changes: 2 additions & 11 deletions bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"log"
"net/url"

"github.com/deanishe/awgo"
"github.com/deanishe/go-safari"
aw "github.com/deanishe/awgo"
safari "github.com/deanishe/go-safari"
)

// Open the bookmark(s)/folder(s) with the specified UIDs.
Expand All @@ -25,10 +25,6 @@ func doOpen() error {
return errors.New("No UID specified")
}

if err := LoadScripts(scriptDirs...); err != nil {
return err
}

a := URLAction(action)
if a == nil {
return fmt.Errorf("Unknown action : %s", action)
Expand Down Expand Up @@ -110,11 +106,6 @@ func filterBookmarks(bookmarks []*safari.Bookmark) error {

showUpdateStatus()

// Load scripts so we can show their icons
if err := LoadScripts(scriptDirs...); err != nil {
return err
}

log.Printf("query=%q", query)

log.Printf("Loaded %d bookmarks", len(bookmarks))
Expand Down
4 changes: 0 additions & 4 deletions cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ func doFilterCloudTabs() error {

showUpdateStatus()

if err := LoadScripts(scriptDirs...); err != nil {
return err
}

tabs, err := cloud.Tabs()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4442,7 +4442,7 @@ Filter all bookmark folders</string>
<string>Open in Current Tab</string>
</dict>
<key>version</key>
<string>0.7.0</string>
<string>0.7.1</string>
<key>webaddress</key>
<string>https://github.com/deanishe/alfred-safari-assistant/blob/master/README.md</string>
</dict>
Expand Down
58 changes: 35 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/deanishe/awgo/update"
"github.com/deanishe/awgo/util"
safari "github.com/deanishe/go-safari"
"github.com/pkg/errors"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

Expand All @@ -43,25 +44,25 @@ const (
// Icons
var (
IconActions = &aw.Icon{Value: "icons/actions.png"}
IconDefault = &aw.Icon{Value: "icon.png"}
IconTab = &aw.Icon{Value: "icons/tab.png"}
IconActive = &aw.Icon{Value: "icons/tab-active.png"}
IconReadingList = &aw.Icon{Value: "icons/reading-list.png"}
IconBlacklistAdd = &aw.Icon{Value: "icons/blacklist-add.png"}
IconBlacklistEdit = &aw.Icon{Value: "icons/blacklist-edit.png"}
IconBookmark = &aw.Icon{Value: "icons/bookmark.png"}
IconBookmarklet = &aw.Icon{Value: "icons/bookmarklet.png"}
IconCloud = &aw.Icon{Value: "icons/cloud.png"}
IconDefault = &aw.Icon{Value: "icon.png"}
IconFolder = &aw.Icon{Value: "icons/folder.png"}
IconGitHub = &aw.Icon{Value: "icons/github.png"}
IconHelp = &aw.Icon{Value: "icons/help.png"}
IconHistory = &aw.Icon{Value: "icons/history.png"}
IconHome = &aw.Icon{Value: "icons/home.png"}
IconReadingList = &aw.Icon{Value: "icons/reading-list.png"}
IconTab = &aw.Icon{Value: "icons/tab.png"}
IconURL = &aw.Icon{Value: "icons/url.png"}
IconFolder = &aw.Icon{Value: "icons/folder.png"}
IconUp = &aw.Icon{Value: "icons/up.png"}
IconHome = &aw.Icon{Value: "icons/home.png"}
IconWarning = &aw.Icon{Value: "icons/warning.png"}
IconHelp = &aw.Icon{Value: "icons/help.png"}
IconBlacklistEdit = &aw.Icon{Value: "icons/blacklist-edit.png"}
IconBlacklistAdd = &aw.Icon{Value: "icons/blacklist-add.png"}
IconGitHub = &aw.Icon{Value: "icons/github.png"}
IconUpdateCheck = &aw.Icon{Value: "icons/update-check.png"}
IconUpdateAvailable = &aw.Icon{Value: "icons/update-available.png"}
IconUpdateCheck = &aw.Icon{Value: "icons/update-check.png"}
IconWarning = &aw.Icon{Value: "icons/warning.png"}
// IconError = &aw.Icon{Value: "icons/error.png"}
)

Expand Down Expand Up @@ -255,6 +256,20 @@ func init() {
blacklistCmd.Arg("scripts", "Names of scripts (without extensions).").
StringsVar(&scriptNames)

// Load action scripts via pre-action
// for _, cmd := range []*kingpin.CmdClause{
// filterCloudTabsCmd, filterTabsCmd, filterTabActionsCmd, filterActionsCmd,
// filterFolderCmd, filterAllFoldersCmd,
// }{
// cmd.PreAction(loadScripts)
// }

app.PreAction(func(ctx *kingpin.ParseContext) error {
if err := LoadScripts(scriptDirs...); err != nil {
return errors.Wrap(err, "load scripts")
}
return nil
})
app.DefaultEnvars()
}

Expand All @@ -263,9 +278,6 @@ func init() {

func doFilterURLActions() error {
log.Printf("URL=%s", actionURL)
if err := LoadScripts(scriptDirs...); err != nil {
return err
}
ua := URLActions()
acts := make([]Actionable, len(ua))
for i, a := range ua {
Expand All @@ -280,10 +292,6 @@ func doURLAction() error {

log.Printf("URL=%s, action=%s", actionURL, action)

if err := LoadScripts(scriptDirs...); err != nil {
return err
}

a := URLAction(action)
if a == nil {
return fmt.Errorf("Unknown action : %s", action)
Expand Down Expand Up @@ -370,13 +378,17 @@ func listActions(actions []Actionable) error {

// run is the main script entry point. It's called from main.
func run() {
var err error

cmd, err := app.Parse(wf.Args())
if err != nil {
wf.Configure(aw.MaxResults(maxResults))

var (
cmd string
err error
)

if cmd, err = app.Parse(wf.Args()); err != nil {
wf.FatalError(err)
}
wf.Configure(aw.MaxResults(maxResults))

// Create user script directories
util.MustExist(filepath.Join(wf.DataDir(), "scripts", "tab"))
Expand Down Expand Up @@ -449,7 +461,7 @@ func run() {
err = doConfig()

default:
err = fmt.Errorf("Unknown command: %s", cmd)
err = fmt.Errorf("unknown command: %s", cmd)

}

Expand Down
2 changes: 1 addition & 1 deletion scripts/url/Open in New Safari Window.png
2 changes: 1 addition & 1 deletion scripts/url/Open in Safari.png
13 changes: 2 additions & 11 deletions tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"net/url"
"strings"

"github.com/deanishe/awgo"
"github.com/deanishe/go-safari"
aw "github.com/deanishe/awgo"
safari "github.com/deanishe/go-safari"
)

// Activate the specified window (and tab).
Expand All @@ -33,9 +33,6 @@ func doFilterTabActions() error {

log.Printf("url=%s, scheme=%s", actionURL, actionURL.Scheme)

if err := LoadScripts(scriptDirs...); err != nil {
return err
}
acts := []Actionable{}
for _, a := range TabActions() {
acts = append(acts, a)
Expand Down Expand Up @@ -100,9 +97,6 @@ func doTabAction() error {
return tab.RunJS(js)
}

if err := LoadScripts(scriptDirs...); err != nil {
return err
}
if actionType == "tab" {
ta := TabAction(action)
if ta == nil {
Expand Down Expand Up @@ -239,9 +233,6 @@ func customTabActions(it *aw.Item) *aw.Item {
bkms[bm.UID()] = bm.Title()
}

if err := LoadScripts(scriptDirs...); err != nil {
wf.FatalError(err)
}
for _, a := range TabActions() {
actions[a.Title()] = "tab"
}
Expand Down

0 comments on commit e8dba3d

Please sign in to comment.