Example scripts
To homepage
Confluence

Synchronize Attachments Between Two Pages
App in script

ScriptRunner For Confluence
by Adaptavist
Compatibility

Confluence (7.15 - 8.6)

ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.atlassian.confluence.event.events.content.attachment.AttachmentEvent
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.AttachmentManager
import com.atlassian.confluence.pages.Page
def spaceSource = '<SPACE_SOURCE_KEY>'
def pageSource = '<PAGE_SOURCE_TITLE>'
def spaceDest = '<SPACE_DEST_KEY>'
def pageDest = '<PAGE_DEST_TITLE>'
def attachmentEvent = event as AttachmentEvent
//get page
def pageManager = ComponentLocator.getComponent(PageManager)
def attachmentManager = ComponentLocator.getComponent(AttachmentManager)
def destPage = pageManager.getPage(spaceDest, pageDest)
def sourcePage = pageManager.getPage(spaceSource, pageSource)
def currentPage = attachmentEvent.content as Page
if ( !destPage ) {
log.warn "Destination page with title '${pageDest}' not available on the '${spaceDest} space'"
return
}
// get attachment for destination page
def sourceAttach = attachmentManager.getLatestVersionsOfAttachments(sourcePage)
def destAttach = attachmentManager.getLatestVersionsOfAttachments(destPage)
def sourceAttachName = sourceAttach*.nameForComparison
def destAttachName = destAttach*.nameForComparison
// return if the page updated is not the pageSource.
if ( currentPage.title != pageSource ) {
log.warn "The currentpage does not match pageSource"
return
}
// Add newly added attachment
sourceAttachName.each { attach ->
if ( !destAttachName.contains( attach )) {
sourceAttach.each { attachment ->
if ( attachment.nameForComparison == attach ) {
log.warn "Attachment $attach added"
attachmentManager.copyAttachment(attachment, destPage)
}
}
}
}
// Delete newly deleted attachment
destAttachName.each { attach ->
if ( !sourceAttachName.contains( attach )) {
destAttach.each { attachment ->
if ( attachment.nameForComparison == attach ) {
log.warn "Attachment $attach deleted"
attachmentManager.removeAttachmentFromServer(attachment)
}
}
}
}
Having an issue with this script?
Report it here